指向C中的指针数组的指针数组

时间:2021-12-02 19:32:44

I have an API which will take triple pointer as an input as below. extern int myAPI(int ***myData);

我有一个API,它将三指针作为输入,如下所示。 extern int myAPI(int *** myData);

Internally "myData" is treated as array of pointer to array of pointers.

在内部,“myData”被视为指向指针数组的指针数组。

So now in another function, i need to call this myAPI. But i am not able to build the array of pointer to array of pointers. Can you please someone help?

所以现在在另一个函数中,我需要调用myAPI。但我无法构建指针数组的指针数组。你能帮忙吗?

I tried similar to below snippet of code. But seen type mismatch compilation error.

我尝试过类似下面的代码片段。但看到类型不匹配编译错误。

int i[10];
int j[10];
int *k[10];
int *l[10];
int *(*m[])[2];
int a = 0;
for (a = 0; a < 10; a++) {
    k[a] = &(i[a]);
    l[a] = &(j[a]);
}

m[0] = k;
m[1] = l;

a = myAPI(m);

2 个解决方案

#1


3  

So you want an "array 10 of pointer to array of pointer 20 to int" (you have to specify the dimensions for all but functions arguments where you can omit the outermost dimension only).

所以你想要一个“指向指针20的数组的指针数组10到int”(你必须指定除函数参数之外的所有维度,你只能省略最外面的维度)。

That would be:

那将是:

int *(*a[10])[20];

For such constructs, cdecl is very helpful. The line in quotes is the declaration for the tool.

对于这样的结构,cdecl非常有用。引号中的行是该工具的声明。

Note this is what you asked for. Which is not necessarily what you really need. Often such complex constructs are a symptom of a severe design flaw. You might want to reconsider your program code.

请注意,这就是你要求的。这不一定是你真正需要的。通常,这种复杂的构造是严重设计缺陷的症状。您可能想重新考虑您的程序代码。

#2


2  

Try changing the declaration of m to

尝试将m的声明更改为

int **m[2];

#1


3  

So you want an "array 10 of pointer to array of pointer 20 to int" (you have to specify the dimensions for all but functions arguments where you can omit the outermost dimension only).

所以你想要一个“指向指针20的数组的指针数组10到int”(你必须指定除函数参数之外的所有维度,你只能省略最外面的维度)。

That would be:

那将是:

int *(*a[10])[20];

For such constructs, cdecl is very helpful. The line in quotes is the declaration for the tool.

对于这样的结构,cdecl非常有用。引号中的行是该工具的声明。

Note this is what you asked for. Which is not necessarily what you really need. Often such complex constructs are a symptom of a severe design flaw. You might want to reconsider your program code.

请注意,这就是你要求的。这不一定是你真正需要的。通常,这种复杂的构造是严重设计缺陷的症状。您可能想重新考虑您的程序代码。

#2


2  

Try changing the declaration of m to

尝试将m的声明更改为

int **m[2];