Louise
Louise

Reputation: 6723

Pointer to 2D array. Why does this example work?

I have this code example, but I don't understand why changing the values in the array inside outputUsingArray() are changing the original array.

I would have expected changing the values of the array in outputUsingArray() would only be for a local copy of the array.

Why isn't that so?

However, this is the behaviour I would like, but I don't understand why it work.

#include <stdlib.h>
#include <stdio.h>
void outputUsingArray(int array[][4], int n_rows, int n_cols) {
  int i, j;

  printf("Output Using array\n");
  for (i = 0; i < n_rows; i++) {
    for (j = 0; j < n_cols; j++) {
      // Either can be used.
      //printf("%2d ", array[i][j] );
      printf("%2d ", *(*(array+i)+j));
    }
    printf("\n");
  }
  printf("\n");

  array[0][0] = 100;
  array[2][3] = 200;

}

void outputUsingPointer(int (*array)[4], int n_rows, int n_cols) {
  int i, j;

  printf("Output Using Pointer to Array i.e. int (*array)[4]\n");
  for (i = 0; i < n_rows; i++) {
    for (j = 0; j < n_cols; j++) {
      printf("%2d ", *(*(array+i) + j ));
    }
    printf("\n");
  }
  printf("\n");
}

int main() {

  int array[3][4] = { { 0, 1, 2, 3 },
              { 4, 5, 6, 7 },
              { 8, 9, 10, 11 } };

  outputUsingPointer((int (*)[4])array, 3, 4);

  outputUsingArray(array, 3, 4);

  printf("0,0: %i\n", array[0][0]);
  printf("2,3: %i\n", array[2][3]);

  return 0;
}

Upvotes: 2

Views: 885

Answers (4)

C oneil
C oneil

Reputation: 167

Heading ##Hi The following is an example of creating a Two dimensional array and updating and then printing using **

#include&lt;stdio.h&gt;
#include&lt;stdlib.h&gt;

void showArray(double** theArray,int arraySizeX, int arraySizeY){
    int index,index2;

    for(index=0;index&lt;arraySizeX;index++){
        for(index2=0;index2&lt;arraySizeY;index2++){
            printf("The valyee of arr[%d][%d] is %lf  \n",index,index2,theArray[index][index2]);
        }
    }


}

void setArray(double** theArray,int arraySizeX, int arraySizeY){
    int index,index2;

    for(index=0;index&lt;arraySizeX;index++){
        for(index2=0;index2&lt;arraySizeY;index2++){
            theArray[index][index2]=index+index2;
        }
    }


}


void main()
{

    int i, arraySizeX =3,arraySizeY=4;


    double** theArray;
    theArray = (double**) malloc(arraySizeX*sizeof(double*));

    for ( i = 0; i &lt; arraySizeX; i++)  {
        theArray[i] = (double*) malloc(arraySizeY*sizeof(double));
    }


    printf("*****************************\n");
    showArray(theArray, arraySizeX,  arraySizeY);
    printf("*****************************\n");

    setArray(theArray, arraySizeX,  arraySizeY);

    printf("__________________________________\n");
    showArray(theArray, arraySizeX,  arraySizeY);
    printf("__________________________________\n");

    free(theArray);


}

I hope this helps,

Upvotes: 0

Rob Parker
Rob Parker

Reputation: 4196

Arrays in C/C++ are really just pointers (with the math done for you). Unless you explicitly make a copy of the contents of the array, passing an array is only passing a pointer to the first element.

So when you modify the array based on that base pointer, you're modifying the original array contents.

Upvotes: 0

Khaled Alshaya
Khaled Alshaya

Reputation: 96859

Arrays in C/C++ are not passed by value. i.e. there is NO copying involved when you pass an array as a parameter. i.e. array is just a name that references the passed array.

Upvotes: 0

Warty
Warty

Reputation: 7395

passing int[] is really passing the pointer to the first element of the array.
passing int* is passing the pointer to the first element of the array as well.

They're identical.

Since they're both pointing towards the same part of memory, changing one will change the other one.

Upvotes: 4

Related Questions