I know that arrays are similar to pointers in C. Is it safe to say that this means that when I pass an array, say int array[3] = {1, 2, 3}, to another method that changing the values of the passed-in array to int array[3] = {3, 2, 1} change the original array as well? If so, does that mean I essentially don't have to return arrays I pass in due to the fact that any value changes I make to the passed-in array changes the original array's values? (as I understand it, it is in fact impossible to have a function return arrays directly.)
Asked
Active
Viewed 5,680 times
1
-
1Yes, passing an array is just passing its address. – user4098326 Feb 18 '15 at 06:33
-
3No, arrays are not just pointers. Why people keep repeating this factually incorrect statement? – n. m. could be an AI Feb 18 '15 at 06:38
-
possible duplicate of [what is array decaying?](http://stackoverflow.com/questions/1461432/what-is-array-decaying) – n. m. could be an AI Feb 18 '15 at 06:40
-
1Arrays are not even *almost* the same as pointers. And arrays cannot be passed as arguments to functions. Read section 6 of the [comp.lang.c FAQ](http://www.c-faq.com/). – Keith Thompson Feb 18 '15 at 06:50
-
You can't pass an array to a function, so the question is moot. – user253751 Feb 18 '15 at 07:04
-
@JS1 if you think arrays and pointers are almost the same, then you're not an experienced C programmer. Sure, the same syntax works for both, but only because arrays decay to pointers. (And that's not "arrays and pointers are the same", that's "pointers and pointers are the same" because of the decay) – user253751 Feb 18 '15 at 07:22
2 Answers
3
You cannot pass an array to a function that way. When you write this:
void myFunc(int ar[]) {
// ar *looks like* an array?
}
int main() {
int array[3] = {1,2,3};
myFunc(array);
}
the compiler will translate it to this:
void myFunc(int *ar) {
// nope, it's actually a pointer...
}
int main() {
int array[3] = {1,2,3};
myFunc(&array[0]); // ... to the first element
}
I recommend not using the void myFunc(int ar[])
syntax, since it only causes confusion. (Writing array
instead of &array[0]
is acceptable, but only because it's shorter.)
As for your question: since you're actually passing a pointer to the array, then yes, modifying the array the pointer points to will modify the original array (because they're the same array).

user253751
- 57,427
- 7
- 48
- 90
0
int array[3] = {1,2,3};
myFunc(array);// you are actually passing the base address of the array to myFunc.
your function declaration of myFunc()
void myFunc(int arr[])
{
}
But it is always good to pass the size of the array to the function along with the base address of array,this will help in avoiding array bound errors.

GingerJack
- 3,044
- 1
- 17
- 19
-
Reading n.m. question that he commented made me arrive at a follow-up question: Can I use sizeof on the array when it's passed to myFunc? – user3735278 Feb 18 '15 at 06:45
-
@user3735278 C is confusing in this sense, but a function parameter `int arr[]` is *adjusted* to `int *arr`. So all you have in such a function is a pointer. All array size information is lost. – juanchopanza Feb 18 '15 at 06:48
-
It is a bit tricky, as the passed array still can be changed using array syntax and, as I know now, changing the array in another function changes the original as well. I was just trying to wrap my head around a problem I was having today, and I just realized that it was simpler than I made it out to be - this answer confirmed that. – user3735278 Feb 18 '15 at 06:51
-
@user3735278 Also note that if your function parameter is *pointer to array*, `void foo(const int (*arr)[10])`, then `sizeof(*arr)` gives the size of the array. But in that case you know its length anyway so there isn't much gain. And C compilers will mostly let you make the mistake of passing an array of the wrong size. – juanchopanza Feb 18 '15 at 06:56