Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

External Delaration for An Array?

Tags:

c

I have an array defined in a file and in another I have to use it, for e.g-

/* a.c - defines an array */  int a[] = {1,2,3,4,5,6,7,8,9};    /* b.c - declare and use it. */  #define COUNT ((sizeof a)/(sizeof int)) extern int a[];  //size of array  . . .  int i; for(i=0; i<COUNT; i++)   printf("%d", a[i]); . . . 

Now when I try to compile it it gave me error saying that sizeof cann't be used on incomplete type.

Can anybody tell me how to handle such case in C/C++? I don't want to array subscript in a.c

Thanks in advance

like image 395
Ravi Gupta Avatar asked Jun 18 '10 17:06

Ravi Gupta


People also ask

Can we extern an array in C?

c? A: An extern array of unspecified size is an incomplete type; you cannot apply sizeof to it. sizeof operates at compile time, and there is no way for it to learn the size of an array which is defined in another file.

What is the declaration of array?

An "array declaration" names the array and specifies the type of its elements. It can also define the number of elements in the array. A variable with array type is considered a pointer to the type of the array elements.

What is extern char in C?

the extern keyword is used to extend the visibility of variables/functions. Since functions are visible throughout the program by default, the use of extern is not needed in function declarations or definitions. Its use is implicit. When extern is used with a variable, it's only declared, not defined.

How do you declare an array in C++?

A typical declaration for an array in C++ is: type name [elements]; where type is a valid type (such as int , float ...), name is a valid identifier and the elements field (which is always enclosed in square brackets [] ), specifies the length of the array in terms of the number of elements.


2 Answers

You might put something like the following into a.c and then extern it from b.c.

In a.c:

int a[] = {1, 2, 3}; const int lengthofa = sizeof( a ) / sizeof( a[0] ); 

And then in b.c:

extern int a[]; // the extern (thanks Tim Post) declaration means the actual storage is in another  // module and fixed up at link time.  The const (thanks Jens Gustedt) prevents it // from being modified at runtime (and thus rendering it incorrect). extern const int lengthofa;  void somefunc() {   int i;   for ( i = 0; i < lengthofa; i++ )     printf( "%d\n", a[i] ); } 
like image 144
Mark Wilkins Avatar answered Oct 10 '22 12:10

Mark Wilkins


If you want your array size to be accessible as a compile-time constant, then you have no other choice but to specify array size explicitly in the extern declaration of the array

extern int a[9]; 

In this case it becomes your responsibility to make sure that array size is consistent between the extern declaration and definition. You can use a manifest constant for that, but still it is going to be your responsibility to make sure that the number of initializers between the {} and the declared size are the same.

If you don't care to have the array size as a compile-time constant, then you can do what Mark Wilkins suggests in his answer.

like image 26
AnT Avatar answered Oct 10 '22 12:10

AnT