How to declare extern 2d-array in header?
Asked Answered
E

7

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
Emmer answered 20/12, 2011 at 20:31 Comment(0)
C
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.

Courageous answered 20/12, 2011 at 20:34 Comment(0)
I
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.)

Il answered 20/12, 2011 at 20:35 Comment(3)
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).Ng
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?Il
Yes, you'are right, but as always better to confirm it (e.g. for some novice programmers, that are reading SO).Ng
S
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];
Sarson answered 20/12, 2011 at 20:36 Comment(0)
B
1

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

extern unsigned char LCD[][64];

Should do it.

Bought answered 20/12, 2011 at 20:36 Comment(0)
R
0

Add to the header file a declaration like:

extern unsigned char LCD[8][64];
Retroactive answered 20/12, 2011 at 20:34 Comment(0)
F
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 !

Finny answered 10/11, 2014 at 10:27 Comment(0)
I
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; }

Inexpedient answered 3/7, 2021 at 10:17 Comment(1)
What does this code do? Read How to Answer and edit your answer.Southerly

© 2022 - 2024 — McMap. All rights reserved.