jacob navia <ja***@jacob.remcomp.frwrites:
re****************@gmail.com wrote:
>I would like to have an array declaration where the size of the array
is dependent on a variable. Something like this:
/* Store the desired size of the array in a variable named
"array_size". */
unsigned short int array_size = 25;
/*Declare an array named "numbers" using the variable initialized
above. */
unsigned short int numbers[array_size];
I think that this syntax will work. However, my real problem is
this. I
need to include this array in a struct definition that will be in a
header file. Am I allowed to have the "array_size" variable
initialization statement in my header file, and if not, how do I
indicate that the size of the array in my struct is based on a
variable, and not a literal number?
Thanks for the help.
Scott Huey
You can declare a structure like this in standard C:
struct M {
int size;
int data[];
};
This is a structure that ends with a flexible array, i.e. an
array of an underminate size.
What jacob isn't bothering to tell you is that this feature is
specific to C99 (the 1999 ISO C standard). It is likely not to be
supported by many current compilers; using it will limit the
portability of your code.
Flexible array members are intended to legitimize a common trick known
as the "struct hack". It's been said that use of the struct hack
*might* invoke undefined behavior, but I'd be surprised to see an
implementation that doesn't support it.
The idea is that you declare a structure like this:
struct M {
int size;
int data[1];
};
and then use malloc() to allocate enough space for a struct M plus
however many additional int elements you need. Here's a brief example:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
struct M {
int size;
int data[1];
};
int count = 25;
int i;
struct M *ptr = malloc(sizeof *ptr + (count-1) * sizeof(int));
ptr->size = count;
for (i = 0; i < count; i ++) {
ptr->data[i] = i;
}
printf("ptr->size = %d\n", ptr->size);
for (i = 0; i < count; i ++) {
printf("ptr->data[%d] = %d\n", i, ptr->data[i]);
}
return 0;
}
A cleaner alternative is to use a pointer to (the first element of) an
array rather than putting the (flexible) array inside the structure
itself:
struct M {
int size;
int *data_ptr;
};
This means you have to allocate two separate objects, making memory
management more difficult.
What you want to do is, however:
struct M {
int size;
int data[size];
};
but this is not allowed. It would be a nice extension of
the language, since variable length structures make no sense
in most cases without some size data. But the standard has
not provided any way to do this.
Probably because it would be difficult (not impossible) to nail down
all the corner cases. I can think of several questions that would
have to be answered; the answers are far from obvious.
--
Keith Thompson (The_Other_Keith)
ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.