Recently, I found an interesting code snippet about array and pointers I didn't understand as a whole.
Here's the code:
void Function(int *B, int *A, int *C, int m)
{
int i;
A++;
for(i = 0; i < m; i++, B++, A++, C++)
*C=*B + *(A-1);
}
int main()
{
int A[] = {3,6,2};
int B[] = {5,7,9};
int C[3];
int n = 3;
int i;
Function(A,B,C,n);
for(i = 0; i < n; i++)
printf("%d ",C[i]);
return 0;
}
Okay, so, in the main, we have two arrays, variable n which we're forwarding to the function. *B, *A, *C are pointers to the first element of an array. First, we have A++, which means that now, A shows on the second element of an array, which is 7. In the first for loop, i = 0, then *C(first element) = 3 + 5 = 8
Then, i = 1, B is now pointing on 6, A on 9.
Next, the second for loop looks like this: *C(second element) = 6 + 7 = 13 Till this part, i understood. Now, i incremented to 2, B is showing on 2, but then, we have A++ again, but how A will increment when there is no other element in that array, it's blank basically? When i debugged, in third for loop, A will be 9(A-1), but again, not completely sure how that work.