10

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
chrisaycock
  • 36,470
  • 14
  • 88
  • 125
user1106072
  • 331
  • 2
  • 4
  • 8

7 Answers7

22

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.

Mark Wilkins
  • 40,729
  • 5
  • 57
  • 110
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.)

Mike Nakis
  • 56,297
  • 11
  • 110
  • 142
  • Might be useful to stress `#include "LCD.h"` into `LCD.c` (so `MINOR` macro is defined only once in header file) in order to exclude possibility of conflicting types for `LCD` between declaration and actual definition (which is not detected as compilation error when they are in different files and linker error as well). – Grzegorz Szpetkowski Jul 15 '14 at 15:16
  • 1
    I thought that whenever a project contains both a XYZ.h and an XYZ.c, it is pretty much a universal rule and a safe bet that XYZ.c will be including XYZ.h, no? – Mike Nakis Jul 15 '14 at 19:35
  • 1
    Yes, you'are right, but as always better to confirm it (e.g. for some novice programmers, that are reading SO). – Grzegorz Szpetkowski Jul 15 '14 at 19:39
2

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];
grzkv
  • 2,599
  • 3
  • 26
  • 37
1

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

extern unsigned char LCD[][64];

Should do it.

embedded_guy
  • 1,939
  • 3
  • 24
  • 39
0

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 !

0

#include using namespace std;

int main(){

int n;
cout<<"Enter The Number"<<endl;
cin>>n;
int m;
cout<<"Enter The Second Number"<<endl;
cin>>m;

int array[n][m];

for (int i = 0; i < n; i++)
{
    for (int j = 0; j < m; j++)
    {
     cin<<array[i][j]<<endl;
    }
    
}

for (int i = 0; i < n; i++)
{

 for (int j = 0; j < m; j++)

 {
    cout<<array[i][j]<<" ";
 }

 cout<<endl;
}

return 0; }

0

Add to the header file a declaration like:

extern unsigned char LCD[8][64];
wallyk
  • 56,922
  • 16
  • 83
  • 148