Calum Murray Calum Murray - 3 months ago 12
C Question

Prototype to for updating pointer to 2D array with unknown dimensions

I want to define an interface that takes a pointer to a pointer to an array of unknown size. The function body should populate this array with an already statically-allocated array.

The problem is, the array I'm passing is part of a 2D array with one dimension unknown:

T_MY_STRUCT (*pMyConfigArray)[][2];


I only want to update along the known dimension per call to the interface.

And the pre-allocated array is defined in another file (the reason for the interface) as follows (The dimension with
2
is known, but the
10
is unknown by the calling side):

T_MY_STRUCT preAllocatedArray[2][10] =
{
{
{0,TRUE,0,1,&dummyParam[0]},
{1,TRUE,1,1,&dummyParam[1]},
{2,TRUE,2,1,&dummyParam[2]},
{3,TRUE,3,1,&dummyParam[3]},
{4,TRUE,4,1,&dummyParam[4]},
{5,TRUE,5,1,&dummyParam[5]},
{6,TRUE,6,1,&dummyParam[6]},
{7,TRUE,7,1,&dummyParam[7]},
{8,TRUE,8,1,&dummyParam[8]},
{9,TRUE,9,1,&dummyParam[9]}
},
{
{0,TRUE, 0,1,&dummyParam[0]},
{1,FALSE,1,1,&dummyParam[1]},
{2,TRUE, 2,1,&dummyParam[2]},
{3,FALSE,3,1,&dummyParam[3]},
{4,TRUE, 4,1,&dummyParam[4]},
{5,FALSE,5,1,&dummyParam[5]},
{6,TRUE, 6,1,&dummyParam[6]},
{7,FALSE,7,1,&dummyParam[7]},
{8,TRUE, 8,1,&dummyParam[8]},
{9,FALSE,9,1,&dummyParam[9]}
}
};


The interfacing function to allow the structure "copying" is implemented as the following:

int retrieveConfigArray(int arrayIndex, T_MY_STRUCT (**configTab)[])
{
int nbParam = 0;

if ((configTab != NULL_VOID_PTR) && (arrayIndex < 2))
{
*configTab = &(preAllocatedArray[mmsPid]);
nbParam = 10;
}

return nbParam;
}


And I pass the
pMyConfigArray
's address to this function so it can be updated:

retrieveConfigArray(0, &(pMyConfigArray[][0])) /* Error Here */
retrieveConfigArray(1, &(pMyConfigArray[][1])) /* Error Here */


My question is, how can I define these array definitions/declarations as well as the function prototype to get the desired behavior? As above I receive
error: invalid use of array with unspecified bounds
on the line where I pass the array. How can I utilize the array-pointer decaying and address-of operator to pass a pointer to the correct dimension here that matches the interface function prototype?

Answer

So the problem I was having was related to how I declared the pointer to the array pMyConfigArray. I had defined it as a pointer to a 2D array of T_MY_STRUCTS structs:

T_MY_STRUCT (*pMyConfigArray)[][2];

In fact what I required was an array of pointers to an array of structs:

T_MY_STRUCT (*pMyConfigArray[])[2] = {NULL, NULL};

This semantic difference uses a different declaration syntax, and must be defined.

This resource in the C Wikibook may prove useful to people in a similar situation. It lists various array/pointer constructs for reference:

int i;         // integer variable 'i'
int *p;        // pointer 'p' to an integer
int a[];       // array 'a' of integers
int f();       // function 'f' with return value of type integer
int **pp;      // pointer 'pp' to a pointer to an integer
int (*pa)[];   // pointer 'pa' to an array of integer
int (*pf)();   // pointer 'pf' to a function with return value integer
int *ap[];     // array 'ap' of pointers to an integer
int *fp();     // function 'fp' which returns a pointer to an integer
int ***ppp;    // pointer 'ppp' to a pointer to a pointer to an integer
int (**ppa)[]; // pointer 'ppa' to a pointer to an array of integers
int (**ppf)(); // pointer 'ppf' to a pointer to a function with return value of type integer
int *(*pap)[]; // pointer 'pap' to an array of pointers to an integer
int *(*pfp)(); // pointer 'pfp' to function with return value of type pointer to an integer
int **app[];   // array of pointers 'app' that point to pointers to integer values
int (*apa[])[];// array of pointers 'apa' to arrays of integers
int (*apf[])();// array of pointers 'apf' to functions with return values of type integer
int ***fpp();  // function 'fpp' which returns a pointer to a pointer to a pointer to an int
int (*fpa())[];// function 'fpa' with return value of a pointer to array of integers
int (*fpf())();// function 'fpf' with return value of a pointer to function which returns an integer