How to declare extern 2d-array in header?

落爺英雄遲暮 提交于 2019-12-10 02:39:44

问题


We have this declaration in LCD.c:

unsigned char LCD[8][64] = {((unsigned char) 0)};

And in LCD.h we want to have something like:

extern unsigned char LCD[][];

We get this error:

Error[Pe098]: an array may not have elements of this type

回答1:


You need, at a minimum, to include the right-most column size for a 2-D array. You can declare it like this:

extern unsigned char LCD[][64];

Otherwise the compiler would not be able to compute the offset after the first row.




回答2:


In C an array does not contain information about the size of each one of its dimensions. Therefore, the compiler needs to know how large is each one of the dimensions except the first one. So, to correct this situation, do something like this:

LCD.h:

#define MINOR 64
extern unsigned char LCD[][MINOR];

LCD.c:

unsigned char LCD[8][MINOR] = {((unsigned char)0)};

(EDIT: sorry, I messed up things in the beginning, fixed it now.)




回答3:


Try specifying the dimensions of the array. In C for a multidimensional array only one dimension can be left unspecified.

Like this:

extern unsigned char LCD[][64];



回答4:


With multidimensional arrays, all but the first dimension must be specified. So...

extern unsigned char LCD[][64];

Should do it.




回答5:


Add to the header file a declaration like:

extern unsigned char LCD[8][64];



回答6:


sizeof of LCD array will refused if you didn't define the size of the two dimension !

sizeof refused : extern unsigned char LCD[][64];
sizeof accepted : extern unsigned char LCD[8][64];

it depend what you want !



来源:https://stackoverflow.com/questions/8581574/how-to-declare-extern-2d-array-in-header

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!