Hi!
Im not sure why the following output of the following code is:
2 15 6 8 10
Code being:
Code:
#include <stdio.h>
void change(int *b, int n) {
int i;
for(i=0; i<n; i++)
*(b+1) = *(b+i)+5;
}
int main() {
int i, a[] = {2, 4, 6, 8, 10};
change(a, 5);
for(i=0; i<=4; i++)
printf("%d ", a[i]);
return 0;
}
I mean, from my understanding an array is basically a pointer of the 1st element of the array, i.e. an array is the address of the first element of an array.
So here *b should be basically a[0] and hence the value of 2 - is this right so far?
if so, then *(b + 1) should be just be basically a[1] and hence 4?
But what is happening then?
it is set to *(b + i) + 5, which should be within the first iteration
*(b + 0) + 5, and hence a[0] + 5 and finally this should be 7?
Im not sure how the calc works to be honest and would be grateful for help to understand it.