By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,710 Members | 1,613 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,710 IT Pros & Developers. It's quick & easy.

Declare an array with a size based on a variable...

P: n/a
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

Aug 1 '06 #1
Share this Question
Share on Google+
8 Replies


P: n/a
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 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.

jacob
Aug 1 '06 #2

P: n/a
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];
If this is really a fixed value, just use a constant value via #define.

Otherwise, my understanding is that you cannot declare and define an
array in terms of another declared int value (with the caveats discussed
else-thread).
Aug 1 '06 #3

P: n/a
C99 allows code like that:
int a=10;
int b[a];

Be careful though. Older compilers
won't compile the above.

--
Papastefanos Serafeim

"Clever Monkey" <cl**************@hotmail.com.invalidwrote in message
news:9S*****************@nnrp.ca.mci.com!nnrp1.uun et.ca...
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];
If this is really a fixed value, just use a constant value via #define.

Otherwise, my understanding is that you cannot declare and define an array
in terms of another declared int value (with the caveats discussed
else-thread).

Aug 1 '06 #4

P: n/a
jacob navia schrieb:
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?

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.
As the above is a C99 feature and the OP may not have a C99
compiler, I suggest reading about the "struct hack":
http://c-faq.com/struct/structhack.html

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Aug 1 '06 #5

P: n/a
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.
Aug 1 '06 #6

P: n/a
Michael Mair <Mi**********@invalid.invalidwrites:
[...]
As the above is a C99 feature and the OP may not have a C99
compiler, I suggest reading about the "struct hack":
http://c-faq.com/struct/structhack.html
Thanks, I would saved some time if I'd found that myself. (I missed
it because the FAQ doesn't use the phrase "struct hack"; it probably
should.)

--
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.
Aug 1 '06 #7

P: n/a
Keith Thompson wrote:
>
>>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.
I mean if we see the structure declaration as a scope (what it is
for the names of the structure members) any unqualified name in the
array index would be looked up in the previously declared members.
If it is found and it is of a type suitable for an array index, then
that would mean that the sizeof() operation in structure M woould be

sizeof(M) + sizeof(size)+typeof(data)*size + alignment;

sizeof(M) is the sizeof operation as applied to this structure i.e.
without the flexible member as specified by C99.

sizeof(size) would be the size of the index type (in this case
sizeof(int).

typeof(data)*size would multiply the type of the flexible array times
the integer expression in the index. This way you could specify:

struct M {
int size;
int data[size+1]; // Integer expression.
};

This is fairly straightforward. What "corner cases" you have
in mind?
Aug 1 '06 #8

P: n/a
jacob navia <ja***@jacob.remcomp.frwrites:
Keith Thompson wrote:
>>>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.

I mean if we see the structure declaration as a scope (what it is
for the names of the structure members) any unqualified name in the
array index would be looked up in the previously declared members.
If it is found and it is of a type suitable for an array index, then
that would mean that the sizeof() operation in structure M woould be

sizeof(M) + sizeof(size)+typeof(data)*size + alignment;
What is "size" in this expression? Is "typeof(data) supposed to be
"sizeof(data)"?
sizeof(M) is the sizeof operation as applied to this structure i.e.
without the flexible member as specified by C99.

sizeof(size) would be the size of the index type (in this case
sizeof(int).

typeof(data)*size would multiply the type of the flexible array times
the integer expression in the index. This way you could specify:

struct M {
int size;
int data[size+1]; // Integer expression.
};

This is fairly straightforward. What "corner cases" you have
in mind?
Ok, suppose you have:

struct M {
int size;
double data[size];
};
struct M obj = { 100 };

(There's no initializer for the "data" member, so it's set to zeros or
whatever.)

Are you saying that sizeof obj would be
(sizeof(int) + obj.size*sizeof(double))
? What about sizeof(struct M)? What if I then do:

obj.size = 200;

Does this implicitly reallocate the structure? Is the user expected
to reallocate the structure? Does it cause sizeof(obj) to be
inconsistent?

Or does the user have to allocate the object manually, making sure to
get the size right? If so, it's just the struct hack with a light
sprinkling of syntactic sugar.

If you have this:

struct M2 {
int size;
double data1[size];
double data2[size];
};

then data2 will have a non-constant size *and* be at a non-constant
offset.

If this were to be standardized, I'd probably suggest requiring any
member whose value affects the size of any other member to be declared
const; any such members must be initialized to some value when the
object is declared. Presumably you wouldn't be able to have an array
of these things, unless perhaps each element of the array has the same
value for "size". And if you allocate a struct M using malloc(), you
can't set the size without cheating (casting away const).

I'm sure there are plenty of things I haven't thought of.

--
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.
Aug 1 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.