Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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;
}
share|improve this question
    
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 stackoverflow.com/questions/546860/… –  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. –  CagkanToptas Oct 19 '13 at 13:15
show 2 more comments

1 Answer

up vote 2 down vote accepted

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.

share|improve this answer
    
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. 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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.