0

I am currently doing a project where I am going to pass a 2D pointer array to a function. Here is the function implementation:

void
affiche_Tab2D(int *ptr, int n, int m)
{
    if (n>0 && m>0 && ptr!=NULL)
    {
        int (*lignePtr)[m]; // <-- Its my first time to see this declaration

        lignePtr = (int (*)[m]) ptr; // <-- and this too....

        for (int i = 0 ; i < n ; i++)
        {
            for (int j = 0 ; j < m ; j++) 
            {
                printf("%5d ",lignePtr[i][j]);
            }
            printf("\b\n");
        }
    }
}

Notice that ptr is 2D array but it uses a single pointer. Before, I used to pass 2D array using double pointers. In my main I have set up a 2D array (double pointer) and finding ways how to send it to that function.

Here are the function calls I tried which does not work:

int 
main(int argc, char * argv[]) 
{
    int ** numbers_table;
    int i, j;

    numbers_table = (int **)malloc(10 * sizeof(int *));

    for(i = 0; i < 10; i++)
        numbers_table[i] = (int *)malloc(10 * sizeof(int));

    for(i = 0; i < 10; i++)
        for(j = 0; j < 10; j++)
            numbers_table[i][j] = i + j;

    // Failed function call 1
    // affiche_Tab2D(numbers_table, 10, 10);

    // Failed function call 2
    // affiche_Tab2D(&numbers_table, 10, 10);

    for(i = 0; i < 10; i++)
    free(numbers_table[i]);

    free(numbers_table);

    return 0;
}
haccks
  • 97,141
  • 23
  • 153
  • 244
it2051229
  • 327
  • 3
  • 12
  • What is your question? Show your function call. – haccks Oct 19 '13 at 12:58
  • The question is I don't know how to pass my 2D pointer array to the function. I updated the question to show you what I have so far. – it2051229 Oct 19 '13 at 13:08
  • possible duplicate http://stackoverflow.com/questions/546860/passing-arrays-and-matrices-to-functions-as-pointers-and-pointers-to-pointers-in/11274267#11274267 – Ulterior Oct 19 '13 at 13:09
  • No this is not a duplicate, take note that I am not allowed to modify the function parameters. The function parameter should stay as is. – it2051229 Oct 19 '13 at 13:11
  • can you try this? : affiche_Tab2D((*numbers_table)[10], 10, 10); im not sure but it should be OK. – Berke Cagkan Toptas Oct 19 '13 at 13:15
  • @CagkanToptas I tried, I got a compile time error: invalid conversion from 'int' to 'int*' and initializing argument 1 of 'void affiche_Tab2D(int*, int, int) – it2051229 Oct 19 '13 at 13:19
  • [This](http://stackoverflow.com/a/17569578/183120) shows the various ways of passing 2D arrays to a function. – legends2k Oct 19 '13 at 13:22

1 Answers1

2

You can't pass numbers_table to your function because it is of type int ** while your function is expecting int *. Also you can't pass &numbers_table because it is of type int ***.
To pass a pointer to int to your function pass &numbers_table[0][0], having a type int *.

affiche_Tab2D(&numbers_table[0][0], 10, 10);  

As per OP's comment:

I would like to know more about the explanation of the declaration int (*lignePtr)[m] and lignePtr = (int (*)[m]) ptr; Its a bit confusing to understand.

int (*lingPtr)[m] is a pointer to an array (of integers) of m elements.
lignePtr = (int (*)[m]) ptr; is casting ptr to a pointer to array of m elements.

newacct
  • 110,405
  • 27
  • 152
  • 217
haccks
  • 97,141
  • 23
  • 153
  • 244
  • Oh my god, you saved me from hours of head ache. I would like to know more about the explanation of the declaration int (*lignePtr)[m] and lignePtr = (int (*)[m]) ptr; Its a bit confusing to understand. – it2051229 Oct 19 '13 at 13:21
  • Wait. Updating my answer. – haccks Oct 19 '13 at 13:24
  • 2
    **Nitpick:** Accessing a 2D array as a 1D array (array flattening) is [undefined behaviour](http://stackoverflow.com/a/7785116/183120). That said most implementations seem to work fine as they don't do array bounds check. – legends2k Oct 19 '13 at 13:25
  • 1
    @legends2k; Thanks for the link. Never knew that. – haccks Oct 19 '13 at 13:36