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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2468214/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 04:53:16  来源:igfitidea点击:

C error Array: array type has incomplete element type.

carrays

提问by ambika

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?

这是正确的方法吗?

回答by David Rodríguez - dribeas

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*Mcharacters (&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 Npointers 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字符 ( &p[0][0]+M == &p[1][0]and (void*)&p[0][0]==(void*)&p[0])的连续内存块的指针,而在第二种情况下,它将期望一个指向内存块的N指针,该内存块包含指向内存块的指针,这些内存块可能或不连续(&p[0][0]并且&p[1][0]不相关并且p[0]==&p[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"

回答by slartibartfast

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 个字符数组的指针,它是您的原型的正确类型 - 但您的char [][M]也很好(实际上它是更常见的公式)。