what is the differene of passing an array and passing a pointer to a function? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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) { }

22nd Jan 2017, 2:04 PM
Hassan
Hassan - avatar
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.
26th Jan 2017, 11:48 AM
Abhinandan Jain
Abhinandan Jain - avatar
- 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.
22nd Jan 2017, 4:52 PM
nick