#include <stdio.h>
void add_adjacents() {
int num1[5] = {1, 2, 3, 4, 5};
int num2[5] = {10, 20, 30, 40, 50};
int final[5];
for (int i=0; i<sizeof(num1); i++) {
final[i] = num1[i] + num2[i];
}
for (int c=0; c<sizeof(final)/sizeof(final[0]); c++) {
printf("%d\n", final[c]);
}
}
void main() {
add_adjacents();
}
所以,我在没有指针的情况下完成了上述操作。但是对于指针,这是我的尝试:我对指针还是陌生的,而且我正在玩不同的练习题。
#include <stdio.h>
void add_adjacents() {
int num1[5] = {1, 2, 3, 4, 5};
int num2[5] = {10, 20, 30, 40, 50};
int final[5];
for (; *num1 != '\0'; *num1++) {
*final = *num1 + *num2;
}
for (int c=0; c<sizeof(final)/sizeof(final[0]); c++) {
printf("%d\n", final[c]);
}
}
void main() {
add_adjacents();
}
下面的技巧:
void add_adjacents() {
int num1[5] = {1, 2, 3, 4, 5};
int num2[5] = {10, 20, 30, 40, 50};
int final[5], c;
int *n1= num1, *n2=num2, *f=final;
for (; n1<&num1[5]; ) {
*f++ = *n1++ + *n2++;
}
for (c=0; c<sizeof(final)/sizeof(final[0]); c++) {
printf("%d\n", final[c]);
}
}
我是一名优秀的程序员,十分优秀!