/* * simple example of for loop and array, revised to show some things * about arrays versus pointers */ #include #define N 10 void set_array(int sz, int a[]); void print_array(int sz, int a[]); int main(void) { int nums[N]; set_array(N, nums); print_array(N, nums); int index; printf("enter index\n"); if (scanf("%d", &index) != 1) { printf("error\n"); return 1; } nums[index] = -1; printf("%d\n", nums[index]); print_array(N, nums); return 0; } /* * print values in array indexing with pointer rather than index * (totally contrived!) */ void print_array(int sz, int a[]) { for (int i = 0; i < sz; ++i) { printf("[%d] %d\n", i, *(a+i)); } } /* * observe that we can change the values in the array -- seems like it * contradicts "pass by value" but no, since what's passed is pointer */ void set_array(int sz, int a[]) { for (int i = 0; i < sz; ++i) { a[i] = i+1; } }