The following code is trying to make a point (probably the difference between arrays and local variables) I only have a vague idea on the code. Would someone please elaborate? Thanks
void doit(int x[10], int y) {
y = 30;
x[0] = 50;
}
void main(void) {
int x[10];
int y = 3;
x[0] = 5;
printf("x[0] is %d and y is %d\n", x[0], y);
doit(x, y);
printf("x[0] is %d and y is %d\n", x[0], y);
}
It is showing that arrays are not really passed directly to functions – instead, the address of the first member of the array is passed.
That means that if the called function modifies the array that was “passed”, it is modifying the original array in the caller.
On the other hand, when the called function modifies the plain
intparametery, it is modifying a local copy of the variable, and the change is not reflected in the caller.