Passing an array as an argument to a function from a function that takes it as an argument in C

G'day!

If I have a function that takes an array of ints as an argument, and then inside this function, send the same array to another function, will it still be able to edit the array values ​​and execute them at the main level, and not at the function level ?

i.e

int main(int argc, char *argv[]) { int A[50]; functionB(A); } 

where function B looks like this:

 void functionB(int A[]) { functionC(A); } 

and the function C is the one that actually mutates the values ​​inside A [].

Will the main look of the modified array or original be A []?

Thanks!

+7
c immutability arrays pointers mutability
source share
1 answer

The array decays to a pointer. Therefore, it will change the original array.

Check out

 void functionC(int A[]) { A[0] = 1; A[1] = 2; } void functionB(int A[]) { functionC(A); } int main(int argc, char *argv[]) { int A[2]={5,5}; printf("Before call: %d %d\n",A[0],A[1]); functionB(A); printf("After call : %d %d\n",A[0],A[1]); } 
+10
source share

All Articles