July 13, 2003
My C book say that we can to declare an array without specify the size

Like this :

char arraywithnosize [];

But i get an error when i compile it : "size of array is not know"

My book is bad ?


July 14, 2003
noobi schrieb...
> My C book say that we can to declare an array without specify the size
> 
> Like this :
> 
> char arraywithnosize [];
> 
> But i get an error when i compile it : "size of array is not know"
> 
> My book is bad ?

You can't allocate a array with no size specified. But you can use this in other situations:

1) declare id as external array

  extern char array[];

This tells the compiler that the name array exist in another file and that it is an array (and not a pointer!)

2) declare it as a function parameter

  void DoIt(int size, char array[]);

This is equivalent to

  void DoIt(int size, char *array);

3) declare an open array as last member of a struct

   struct Data {
      int  size;
      char array[];
   };

Now it's possible to dynamically allocate storage. Example:

   #define ASIZE 200
   int i;
   struct Data *my_data = malloc(sizeof(struct Data)
                                 + ASIZE*sizeof(char));
   /* Initialize it */
   my_data->size = ASIZE;
   for(i=0; i<ASIZE; ++i) my_data->array[i]=0;


Hope this helps,

- Heinz