Passing an array as an argument to a function in C

I wrote a function containing an array as an argument, and call it by passing the value of the array as follows.

void arraytest(int a[]) { // changed the array a a[0]=a[0]+a[1]; a[1]=a[0]-a[1]; a[0]=a[0]-a[1]; } void main() { int arr[]={1,2}; printf("%d \t %d",arr[0],arr[1]); arraytest(arr); printf("\n After calling fun arr contains: %d\t %d",arr[0],arr[1]); } 

I found that although I call the arraytest() function, passing the values, the original copy of int arr[] changes.

Could you explain why?

+47
c arrays
Jul 04 2018-11-11T00:
source share
8 answers

When passing an array as a parameter, this

 void arraytest(int a[]) 

means the same as

 void arraytest(int *a) 

so you change the values ​​basically.

For historical reasons, arrays are not first-class citizens and cannot be transferred by value.

+65
Jul 04 2018-11-11T00:
source share

You do not pass the array as a copy. This is only a pointer pointing to the address where the first element in memory is located.

+6
Jul 04 2018-11-11T00:
source share

You pass the address of the first element of the array

+6
Jul 04 2018-11-11T00:
source share

In C, with the exception of a few special cases, an array reference always breaks up to a pointer to the first element of the array. Therefore, it is not possible to pass an array "by value". The array in the function call will be passed to the function as a pointer, which is similar to passing the array by reference.

EDIT: There are three such special cases where an array does not decay into a pointer to the first element:

  • sizeof a does not match sizeof (&a[0]) .
  • &a is not the same as &(&a[0]) (and not exactly the same as &a[0] ).
  • char b[] = "foo" does not match char b[] = &("foo") .
+6
Jul 04 '11 at 6:10
source share

You pass the value of the memory cell of the first member of the array.

Therefore, when you start modifying an array inside a function, you modify the original array.

Remember that a[1] - *(a+1) .

+4
Jul 04 2018-11-11T00:
source share

If you want to pass a one-dimensional array as an argument to a function , you will have to declare the formal parameter in one of the following three ways, and all three declaration methods give similar results, because each tells the compiler that an integer pointer will be accepted .

 int func(int arr[], ...){ . . . } int func(int arr[SIZE], ...){ . . . } int func(int* arr, ...){ . . . } 

So, you are changing the initial values.

Thank!!!

+2
May 05 '16 at 9:05
source share

Arrays in C are in most cases converted to a pointer to the first element of the array itself. And in more detail, arrays passed to functions are always converted to pointers.

Here is a quote from K & R2nd :

When the array name is passed to the function, the location of the source element is passed. Inside the function being called, this argument is a local variable, so the parameter of the array name is a pointer, that is, a variable containing the address.

Record:

 void arraytest(int a[]) 

has the same meaning as the entry:

 void arraytest(int *a) 

Therefore, despite the fact that you are not writing it explicitly, this happens when you pass a pointer, and therefore you change the values ​​basically.

For more, I really suggest reading this .

Alternatively, you can find other answers to SO here.

+1
Apr 30 '17 at 10:53 on
source share

Passing a multidimensional array as an argument to a function. Passing one dull array as an argument is more or less trivial. Let me take a look at a more interesting case of passing a 2-dimensional array. In C, you cannot use a pointer to a pointer construct (int **) instead of a 2 dim array. Here is an example:

 void assignZeros(int(*arr)[5], const int rows) { for (int i = 0; i < rows; i++) { for (int j = 0; j < 5; j++) { *(*(arr + i) + j) = 0; // or equivalent assignment arr[i][j] = 0; } } 

Here I pointed out a function that takes a pointer to an array of 5 integers as the first argument. I can pass any 2-dimensional array that has 5 columns as an argument:

 int arr1[1][5] int arr1[2][5] ... int arr1[20][5] ... 

You can come up with a more general function that can take any 2-dimensional array and change the function signature as follows:

 void assignZeros(int ** arr, const int rows, const int cols) { for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { *(*(arr + i) + j) = 0; } } } 

This code will be compiled, but you will get a runtime error when trying to assign values ​​in the same way as in the first function. So in C multidimensional arrays do not match pointers to pointers ... pointers. Int (* arr) [5] is a pointer to an array of 5 elements, int (* arr) [6] is a pointer to an array of 6 elements, and they are pointers to different types!

Well, how do you define function arguments for higher dimensions? Simple, we just follow the pattern! Hier ist the same function configured to select an array of three dimensions:

 void assignZeros2(int(*arr)[4][5], const int dim1, const int dim2, const int dim3) { for (int i = 0; i < dim1; i++) { for (int j = 0; j < dim2; j++) { for (int k = 0; k < dim3; k++) { *(*(*(arr + i) + j) + k) = 0; // or equivalent assignment arr[i][j][k] = 0; } } } } 

As you would expect, as an argument, it can take any 3-dimensional arrays that have 4 elements in the second dimension and in the elements of the third dimension 5. Everything that would be in this would be good:

 arr[1][4][5] arr[2][4][5] ... arr[10][4][5] ... 

But we must indicate all sizes before the first.

0
Feb 21 '18 at 21:48
source share



All Articles