用于更新指向未知维度的{2D}数组的指针的原型

时间:2016-09-05 11:22:19

标签: c arrays multidimensional-array

我想定义一个接口,该接口指向指向未知大小数组的指针。函数体应该使用已经静态分配的数组填充此数组。

问题是,我传递的数组是2D数组的一部分,其中一维未知:

T_MY_STRUCT (*pMyConfigArray)[][2];

我只想在每次调用界面时更新已知维度。

预先分配的数组在另一个文件中定义(接口的原因),如下所示(2的维度已知,但调用方未知10):

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]}
  }
};

接口功能允许结构"复制"实现如下:

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;
}

我将pMyConfigArray的地址传递给此功能,以便更新:

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

我的问题是,如何定义这些数组定义/声明以及函数原型以获得所需的行为?如上所述,我收到了 我传递数组的行上的error: invalid use of array with unspecified bounds。如何利用数组指针衰减和address-of运算符将指针传递给匹配接口函数原型的正确维度?

1 个答案:

答案 0 :(得分:0)

所以我遇到的问题与我如何声明指向数组pMyConfigArray的指针有关。我已将其定义为指向T_MY_STRUCT结构的2D数组的指针:

T_MY_STRUCT (*pMyConfigArray)[][2];

实际上我需要的是一个指向结构数组的指针数组:

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

这种语义差异使用不同的声明语法,必须初始化。

C Wikibook中的此资源可能对类似情况下的人有用。它列出了各种数组/指针结构供参考:

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