0

I have a 2D array of pointer

main.c

Int32 * pRxSeqAddr[2][2];

func(pRxSeqAddr);

/

func.c

void func( Int32** pRxSeqAddrPtr)
{

///
}

I get this error:

argument of type "Int32 *(*)[2]" is incompatible with parameter of type "Int32 **

I know if it was 1D array of pointer then this function call is fine, but 2D is nagging me..

please help

Paul R
  • 208,748
  • 37
  • 389
  • 560
user437777
  • 1,423
  • 4
  • 17
  • 28

4 Answers4

5

Change func.c to:

void func(Int32 *pRxSeqAddrPtr[][2])
{

}

In general, to pass 2D arrays to functions you need to specify the array dimensions for all but the first dimension, so in this example you need the [2] for the last dimension but you can optionally omit it for the first dimension, i.e.

void func(Int32 *pRxSeqAddrPtr[2][2])
{

}

would also be valid, but somewhat redundant.

Paul R
  • 208,748
  • 37
  • 389
  • 560
  • Hi Paul, Thanks it works... but I do not know how to interpret the logic behind this.. – user437777 Nov 11 '10 at 10:17
  • @user437777: OK - I've added a little more clarification - HTH – Paul R Nov 11 '10 at 10:30
  • Hi Paul, thanx for clarification... I checked the values it passed inside the function. It has passed to me addresses of 1st and 2nd column correctly. To get the rows addresses, do i need to manually extract using somekind of pointer offset? – user437777 Nov 11 '10 at 11:27
  • 1
    @user437777: if you really need to know the *address* of a given row, `i`, then it's just `pRxSeqAddrPtr[i]`, or `&pRxSeqAddrPtr[i][0]`, whichever you prefer. However you probably don't really need to know this - what is it you're trying to do exactly ? – Paul R Nov 11 '10 at 11:37
4

Except when it is the operand of the sizeof or unary & operators or is a string literal being used to initialize another array, an expression of type "N-element array of T" will be implicitly converted ("decay") to type "pointer to T", and its value will be the address of the first element of the array.

When you make the call to func(pRxSeqAddress), the type of the expression pRxSeqAddress is converted from "2-element array of 2-element array of pointer to Int32" to "pointer to 2-element array of pointer to Int32", or Int32 *(*)[2]. Thus, the prototype for func should be

void func(Int32 *(*pRxSeqAddressPtr)[2]) { ... }

In the context of a function parameter declaration, T a[] and T a[N] are both synonymous with T *a; a is actually of pointer type, not an array type. So the above could be rewritten as

void func(Int32 *pRxSeqAddressPtr[][2]) { ... }

which looks a little cleaner than the pointer to array syntax; however, I prefer the former as it describes exactly what's going on. Note that this is only true in the context of a function parameter declaration.

So, given the declaration T a[N][M];, the following all hold true

Expression        Type         Decays to
----------        ----         ---------
         a        T [N][M]     T (*)[M]
        &a        T (*)[N][M]  n/a
        *a        T [M]        T *
      a[i]        T [M]        T *
     &a[i]        T (*)[M]     n/a
     *a[i]        T            n/a
   a[i][j]        T            n/a

Note that it's the type of the expression referring to the array that changes, not the array itself; the object pRxSeqAddr defined in main is always and forever of array type.

John Bode
  • 119,563
  • 19
  • 122
  • 198
-2

This will surely work:

Int32* pRxSeqAddr[2*2];
func(pRxSeqAddr);

void func(Int32** pRxSeqAddrPtr){};

Or, in your original example, you could call the function with

funct( &pRxSeqAddr[0][0] );
ruslik
  • 14,714
  • 1
  • 39
  • 40
  • It may "work", but it doesn't really answer the question, as it just avoids the issue of passing 2D arrays. – Paul R Nov 11 '10 at 09:39
  • 1
    Changing the type of the array rather than fixing the parameter seems the wrong way to go about it. – The Archetypal Paul Nov 11 '10 at 09:39
  • @Paul R in your case the function becames locked to size `[2]`. Maybe OP just needed a simple plain pointer in the function? – ruslik Nov 11 '10 at 09:42
  • the OP gave asked a specific question about passing a fixed size 2D array - IMHO it's better to answer the question *as asked* rather than try to second guess what the OP might have wanted instead - he can always clarify the question later if needed. – Paul R Nov 11 '10 at 09:47
  • this ill involve more complex math when it comes to working with the array. – Igbanam Nov 11 '10 at 20:53
-3

In C, by default, 2D array'' arearray of pointers to 1D arrays''.

So, there, you have to name your type : Int32***

First star for the first array dimension. (arrays are pointer to their first item) Second star for the second array dimension. (pointers to first item of the line) Third star because the items are pointers.

Nonyme
  • 1,220
  • 1
  • 11
  • 22