C错误数组:数组类型有不完整的元素类型。

时间:2022-02-09 14:05:00

I have:

我有:

extern int docx(char *,char[][]) // in a header file

It is compiled properly in solaris, but in Redhat Linux it shows the error bellow:

它在solaris中被正确地编译,但是在Redhat Linux中,它显示错误如下:

array type has incomplete element type.

I know i can solve it as - char[][20]

我知道我可以解出- char[][20]

Is it the right way?

这是正确的方法吗?

2 个解决方案

#1


17  

You will have to know what the function is actually expecting and modify the interface accordingly. If it is expecting a bidimensional array (char [N][M]) the correct interface would be:

您必须知道函数实际期望的是什么,并相应地修改接口。如果它期望一个二维数组(char [N][M]),那么正确的接口应该是:

extern int docx(char *,char*[M]);

Which is different from:

这是不同的:

extern int docx( char*, char** );

In the first case the function would be expecting a pointer into a contiguous block of memory that holds N*M characters (&p[0][0]+M == &p[1][0] and (void*)&p[0][0]==(void*)&p[0]), while in the second case it will be expecting a pointer into a block of memory that holds N pointers to blocks of memory that may or not be contiguous (&p[0][0] and &p[1][0] are unrelated and p[0]==&p[0][0])

在第一种情况下函数会预期到一块连续的内存指针包含N * M字符(代替[0][0]+ M = =代替[1][0]和(void *)代替[0][0]= =(void *)[0]代替),而在第二种情况下它将期待一个指向一块内存,N的内存块的指针可能会或不会是连续的(代替[0][0]和[1][0]代替无关和p[0]= =[0][0]代替)

// case 1
ptr ------> [0123456789...M][0123.........M]...[0123.........M]

// case 2
ptr ------> 0 [ptr] -------> "abcde"
            1 [ptr] -------> "another string"
              ...
            N [ptr] -------> "last string"

#2


2  

char *[M] is no different from char **. char *[M] is an array of pointers to char. The dimension plays no role in C (in this case). What David meant was char (*)[M] which is a pointer to an array of M chars which would be the correct type for your prototype - but your char [][M] is fine also (in fact it is the more common formulation).

char *[M]与char **没有什么不同。char *[M]是指向char的指针数组。维度在C中没有作用(在本例中)。David的意思是char (*)[M]是一个指向M chars数组的指针,它是原型的正确类型——但是你的char [][M]也没问题(实际上它是更常见的表达式)。

#1


17  

You will have to know what the function is actually expecting and modify the interface accordingly. If it is expecting a bidimensional array (char [N][M]) the correct interface would be:

您必须知道函数实际期望的是什么,并相应地修改接口。如果它期望一个二维数组(char [N][M]),那么正确的接口应该是:

extern int docx(char *,char*[M]);

Which is different from:

这是不同的:

extern int docx( char*, char** );

In the first case the function would be expecting a pointer into a contiguous block of memory that holds N*M characters (&p[0][0]+M == &p[1][0] and (void*)&p[0][0]==(void*)&p[0]), while in the second case it will be expecting a pointer into a block of memory that holds N pointers to blocks of memory that may or not be contiguous (&p[0][0] and &p[1][0] are unrelated and p[0]==&p[0][0])

在第一种情况下函数会预期到一块连续的内存指针包含N * M字符(代替[0][0]+ M = =代替[1][0]和(void *)代替[0][0]= =(void *)[0]代替),而在第二种情况下它将期待一个指向一块内存,N的内存块的指针可能会或不会是连续的(代替[0][0]和[1][0]代替无关和p[0]= =[0][0]代替)

// case 1
ptr ------> [0123456789...M][0123.........M]...[0123.........M]

// case 2
ptr ------> 0 [ptr] -------> "abcde"
            1 [ptr] -------> "another string"
              ...
            N [ptr] -------> "last string"

#2


2  

char *[M] is no different from char **. char *[M] is an array of pointers to char. The dimension plays no role in C (in this case). What David meant was char (*)[M] which is a pointer to an array of M chars which would be the correct type for your prototype - but your char [][M] is fine also (in fact it is the more common formulation).

char *[M]与char **没有什么不同。char *[M]是指向char的指针数组。维度在C中没有作用(在本例中)。David的意思是char (*)[M]是一个指向M chars数组的指针,它是原型的正确类型——但是你的char [][M]也没问题(实际上它是更常见的表达式)。