Thread: passing a pointer to a 2d array to a function

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    662

    passing a pointer to a 2d array to a function

    i want to populate an array from a function so i don't have the same code 20 times. However i have tried the following and i get a warning about "passing argument 1 of 'populate array' from incompatible pointer type" (line 12.) a note that says "expected 'int *' but argument is of type 'int (*)[8][8]' "line 4. On top of this i have an error saying "undefined reference to 'populate_array' "line 12 also.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void populate_array(int *temp_array);
    
    int main()
    {
        int i, j;
        int temp_array[8][8] = {0};
        //int *p_temp_array = &temp_array;
    
        populate_array(&temp_array);
    
        for (j = 0; j < 8; j++)
        {
            for (i = 0; i < 8; i++)
            {
                printf("%d ", temp_array[i][j]);
            }
            printf("\n");
        }
    
        return 0;
    }
    what am i doing wrong please
    coop

  2. #2
    Registered User
    Join Date
    Apr 2019
    Posts
    57
    First off, let me just give you the answer. From your other post, the specifics of why this is is probably left to later. Don't worry about not understanding things below this code segment yet. When passing 2D arrays, just follow this pattern for now. You can revisit this later when you get some more experience under your belt.

    Code:
    void populate(int a[][8], int n) {
      for(int y = 0; y < n; y++)
        for(int x = 0; x < 8; x++)
          a[y][x] = 42;
    }
    
    int main(void) {
      int array[8][8];
      populate(array, 8);
    }
    Wait, where's the pointer? A quirk of C is that arrays passed to functions become pointers but without the pointer syntax. You don't need to pass the address of an array because that array has become a pointer to that array. You don't need to list the type as a pointer in the function definition because an array in this context is a pointer.

    So why won't your version work? Technically, it will. If you look back you your previous post[1], I showed how a 2D array looks in memory. It's just a 1D array, and passing the address to the beginning of this array should work, right? Not exactly. If you have an 8x8 array, C knows that the 2nd row starts at offset 8 into that array. If you simply pass the address of that 8x8 array to a function, how is C going to know where the offset to the second row is? For example, you cannot do this.

    Code:
    void populate(int *a) {
      a[2][0] = 10;
    }
    
    int main(void) {
      int array[8][8];
      populate((int*)&array);
    }
    You can force the pointer type using a cast, and that will be a valid pointer, but you cannot then use it as a 2D array. Inside the populate function, C doesn't have the information it need to know how large the array is, it won't know where the 2nd or 3rd row starts because it just as a pointer.

    [1] stupid question time

  3. #3
    Registered User
    Join Date
    Apr 2019
    Posts
    2
    Hello cooper1200

    Well first of all in order to pass a 2d array you must define in main 2 pointers (int** temp_array , you must also assign to variables (for example i and j) the size of rows and columns.

    Now since you are using a void function to do the population , you must pass the array, the rows and the columns to that function (array as reference and rows/columns as values, or both as references if you wish). Then in the population function , you are gonna populate and show the the results (printf). Void functions return nothing , this is why they are called void so you must show scan the user input and show the results from that function.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    /* run this program using the console pauser or add your own getch, system("pause") or input loop */
    void populate_array(int** temp_array, int i, int j)
    {
    	//Populate array code
    }
    
    
    int main()
    {
      int i = 8;
      int j = 8;
      int **temp_array;
      
    populate_array(temp_array, i, j);
    }
    Hope this helps.

    Ulothar

  4. #4
    Registered User
    Join Date
    Apr 2019
    Posts
    57
    No. This is not the same as a 2D array. An int** is a pointer to pointers, a 2D array like an int[8][8] is completely different. An int** is what's referred to as a jagged array, it's a number of 1D arrays allocated separately that can be anywhere in memory plus an array of pointers to keep them in order. For example,

    Code:
    int **a = malloc(sizeof *a * 10);
    // At this point, *a is a pointer to 10 pointers to ints
    // These pointers don't point anywhere yet, though
    
    for(int i = 0; i < 10; i++)
      a[i] = malloc(sizeof **a * 10);
    
    // Now each point to a different memory location of 10 ints
    // These are 10 separate, non-contiguous areas of memory
    This is not the same as a 2D array.

    Code:
    int a[8][8];
    
    // a is an array of 64 ints in contiguous memory

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing 2D array to function via pointer of pointer
    By sean_cantab in forum C Programming
    Replies: 4
    Last Post: 05-09-2016, 10:15 AM
  2. Replies: 4
    Last Post: 09-08-2013, 06:17 PM
  3. Passing two dimensional array to function using pointer
    By Burns111 in forum C Programming
    Replies: 3
    Last Post: 11-27-2012, 10:55 AM
  4. Replies: 11
    Last Post: 12-30-2009, 04:04 PM
  5. Passing pointer to array to a function
    By Tojam in forum C Programming
    Replies: 1
    Last Post: 10-09-2002, 09:24 PM

Tags for this Thread