+ 1
what is the differene of passing an array and passing a pointer to a function?
Is the following code the same? void func(int * arr, int size) { } void func(int arr[], int size) { }
2 Answers
0
The array values are stored at some place in the memory. Let's say at location 'x'
If we pass an array to a function . A duplicate set of values would be created at, let's say, location 'y'. And that would be received by the function. The function would modify that set of values. Our original set would remain unchanged.
And we'll have to modify our original set with the values received.
If we pass the pointer. The function would receive the location 'x'. And the changes would occur to the values there. So our original set gets changed.
- 1
the pointer passes the location in memory and passing the array passes the values of the array. passing the location allows you to modify the actual array instead of a copy of the array.