+ 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?

18th Jan 2021, 3:32 PM
DarkTechYon
DarkTechYon - avatar
1 ответ
+ 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.
18th Jan 2021, 4:17 PM
XXX
XXX - avatar