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;
}
7
  • What is your question? Show your function call. Commented Oct 19, 2013 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. Commented Oct 19, 2013 at 13:08
  • possible duplicate stackoverflow.com/questions/546860/… Commented Oct 19, 2013 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. Commented Oct 19, 2013 at 13:11
  • can you try this? : affiche_Tab2D((*numbers_table)[10], 10, 10); im not sure but it should be OK. Commented Oct 19, 2013 at 13:15

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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.
Wait. Updating my answer.
Nitpick: Accessing a 2D array as a 1D array (array flattening) is undefined behaviour. That said most implementations seem to work fine as they don't do array bounds check.
@legends2k; Thanks for the link. Never knew that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.