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

  • Thread starter redefined.horizons
  • Start date
R

redefined.horizons

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
 
J

jacob navia

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
 
C

Clever Monkey

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).
 
P

Papastefanos Serafeim

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 said:
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).
 
M

Michael Mair

jacob said:
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
 
K

Keith Thompson

jacob navia said:
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;
}

printf("ptr->size = %d\n", ptr->size);
for (i = 0; i < count; i ++) {
printf("ptr->data[%d] = %d\n", i, ptr->data);
}
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.
 
J

jacob navia

Keith said:
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?
 
K

Keith Thompson

jacob navia said:
Keith said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top