+ 1
Can I fill an array if I put it in the argument of a function?
I know that in C, if I pass variables through argument,when the function ends, the variables return to normal, but will it be the same for the arrays, or if I fill them in the function, the changes will be brought to the main function? And if it's possible, do I have to write "return (array);" at the end of the function? Or is it not necessary?
1 Respuesta
+ 4
In C, you cannot pass arrays by value. Moreover, you can't even store arrays by value in variables. When you assign an array to a variable, actually the pointer to the first element of the array is assigned. Example
`
int x[] = {1, 2, 3};
printf("%d", *x); //outputs 1
`
In the same way, when you pass arrays to functions, you actually pass the array *pointer*. And for the same reason, when you make changes to an array in a function, it changes the array that was passed to the function too. Why? Because they are the same array. You don't need a return statement for the changes to be there.