Referencing a global array outside a function

F

flipflop

I need to create a global array whose dimensions depend on the
contents of another global array populated at its initialisation. For
example:

int array1[3]={3,2,1};
int array2[array1[0]]; //should be equiv. to: int array2[3];

GCC gives these errors:
variable-size type declared outside of any function
variable-sized object may not be initialized
warning: excess elements in array initializer
warning: (near initialization for `array2')

Both arrays need to be global and created before main() starts. Any
ideas?
 
L

Leor Zolman

I need to create a global array whose dimensions depend on the
contents of another global array populated at its initialisation. For
example:

int array1[3]={3,2,1};
int array2[array1[0]]; //should be equiv. to: int array2[3];

GCC gives these errors:
variable-size type declared outside of any function
variable-sized object may not be initialized
warning: excess elements in array initializer
warning: (near initialization for `array2')

Both arrays need to be global and created before main() starts. Any
ideas?

#include <stdio.h>

#define arrsize(a) (sizeof(a) / sizeof(*a))

int array1[3]={3,2,1};
int array2[arrsize(array1)]; /*should be equiv. to: int array2[3]; */

int main()
{
printf("sizeof(array1) = %d\n", sizeof(array1));
printf("sizeof(array2) = %d\n", sizeof(array2));
return 0;
}

Output:
sizeof(array1) = 12
sizeof(array2) = 12

-leor
 
J

James Hu

I need to create a global array whose dimensions depend on the
contents of another global array populated at its initialisation. For
example:

int array1[3]={3,2,1};
int array2[array1[0]]; //should be equiv. to: int array2[3];

#define ARRAY1_MAX (3)
#define ARRAY1(n) (ARRAY1_MAX - (n))

int array1[3] = { ARRAY1(0), ARRAY1(1), ARRAY1(2) };
int array2[ARRAY1(0)];

-- James
 
J

Jeremy Yallop

flipflop said:
I need to create a global array whose dimensions depend on the
contents of another global array populated at its initialisation. For
example:

int array1[3]={3,2,1};
int array2[array1[0]]; //should be equiv. to: int array2[3];

enum { constant_expression = 3 };
int array1[3] = { constant_expression, 2, 1};
int array2[constant_expression];

Jeremy.
 
M

Martin Dickopp

Leor Zolman said:
I need to create a global array whose dimensions depend on the ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
contents of another global array populated at its initialisation. For ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
example:

int array1[3]={3,2,1};
int array2[array1[0]]; //should be equiv. to: int array2[3];

GCC gives these errors:
variable-size type declared outside of any function
variable-sized object may not be initialized
warning: excess elements in array initializer
warning: (near initialization for `array2')

Both arrays need to be global and created before main() starts. Any
ideas?

#include <stdio.h>

#define arrsize(a) (sizeof(a) / sizeof(*a))

int array1[3]={3,2,1};
int array2[arrsize(array1)]; /*should be equiv. to: int array2[3]; */

int main()
{
printf("sizeof(array1) = %d\n", sizeof(array1));
printf("sizeof(array2) = %d\n", sizeof(array2));
return 0;
}

I don't think that's what the OP wanted (see underlined text). `array2'
shouldn't have 3 elements because `array1' has three elements, but because
the zeroth element of `array1' is 3.

The answer is: it cannot be done directly. One possibility is to define
a macro which expands to `3', and use it to both initialize the zeroth
element of `array1' and define the size of `array2'.

Martin
 
L

Leor Zolman

I don't think that's what the OP wanted (see underlined text). `array2'
shouldn't have 3 elements because `array1' has three elements, but because
the zeroth element of `array1' is 3.

Yup; a case of overloaded 3's ;-)
Thanks,
-leor
 
F

flipflop

Martin Dickopp said:
I don't think that's what the OP wanted (see underlined text). `array2'
shouldn't have 3 elements because `array1' has three elements, but because
the zeroth element of `array1' is 3.

The answer is: it cannot be done directly. One possibility is to define
a macro which expands to `3', and use it to both initialize the zeroth
element of `array1' and define the size of `array2'.

Guys - thanks for your replies. I probably should have asked the
question more simply, as:

" How can you reference a global array element, set during the
array's initialisation, before main() starts? For example:
int array[3]={3,2,1};
int x=array[0];
main() { ... "

It seems it's not possible but I can fudge around it. Thanks again.
(Nice photos by the way Martin.)
 
L

Leor Zolman

Martin Dickopp said:
I don't think that's what the OP wanted (see underlined text). `array2'
shouldn't have 3 elements because `array1' has three elements, but because
the zeroth element of `array1' is 3.

The answer is: it cannot be done directly. One possibility is to define
a macro which expands to `3', and use it to both initialize the zeroth
element of `array1' and define the size of `array2'.

Guys - thanks for your replies. I probably should have asked the
question more simply, as:

" How can you reference a global array element, set during the
array's initialisation, before main() starts? For example:
int array[3]={3,2,1};
int x=array[0];
main() { ... "

Actually, the issues don't change much depending on /what/ you want to use
that value for...the bottom line, as you've acknowledged below, is that C
does not allow dereferencing operations in constant expressions (and array
subscripting is typically translated into code involving pointer
dereferencing.)

I'm not sure why you're so determined to be able to do this, since (as has
been pointed out), it is actually simpler and more direct to just take the
same expression that you've placed into the array initializer list and
reuse it the next time you need it. What is so gosh-darn important about
having to "extract it out of the array", anyway? I've always gotten the
feeling there was more to this problem than you've shown, but that if you
were to fully explain the problem you're trying to solve, ultimately we'd
all conclude that the most straightforward solution wouldn't require this
capability you've been asking about.
-leor
 
F

flipflop

Leor Zolman said:
Guys - thanks for your replies. I probably should have asked the
question more simply, as:

" How can you reference a global array element, set during the
array's initialisation, before main() starts? For example:
int array[3]={3,2,1};
int x=array[0];
main() { ... "

Actually, the issues don't change much depending on /what/ you want to use
that value for...the bottom line, as you've acknowledged below, is that C
does not allow dereferencing operations in constant expressions (and array
subscripting is typically translated into code involving pointer
dereferencing.)

I'm not sure why you're so determined to be able to do this, since (as has
been pointed out), it is actually simpler and more direct to just take the
same expression that you've placed into the array initializer list and
reuse it the next time you need it. What is so gosh-darn important about
having to "extract it out of the array", anyway? I've always gotten the
feeling there was more to this problem than you've shown, but that if you
were to fully explain the problem you're trying to solve, ultimately we'd
all conclude that the most straightforward solution wouldn't require this
capability you've been asking about.

Are you familiar with neural networks? It's related to creating a
population of network structures where the number of real-valued
weights per neuron in a layer depends on the number of neurons in the
layer before it. The number of layers in total is fixed at compile
time but varies within a range. I'm happy to go with my workaround
solution though which, all told, only means I have to do a trivial
amount of redundant hard coding of network values.
 
N

Neil Kurzman

flipflop said:
I need to create a global array whose dimensions depend on the
contents of another global array populated at its initialisation. For
example:

int array1[3]={3,2,1};
int array2[array1[0]]; //should be equiv. to: int array2[3];

GCC gives these errors:
variable-size type declared outside of any function
variable-sized object may not be initialized
warning: excess elements in array initializer
warning: (near initialization for `array2')

Both arrays need to be global and created before main() starts. Any
ideas?

How about?

If you must, then make "array2" a pointer to int and malloc the second
array.

int array1[3]={3,2,1};
int *array2=NULL;

then in main:
array2 = Malloc([array1[0] * sizeof(int);
if (array2 == NULL)hosed = TRUE; // gotta error check

Global and dynamic
 
F

flipflop

Neil Kurzman said:
If you must, then make "array2" a pointer to int and malloc the second
array.

int array1[3]={3,2,1};
int *array2=NULL;

then in main:
array2 = Malloc([array1[0] * sizeof(int);
if (array2 == NULL)hosed = TRUE; // gotta error check

Global and dynamic

I've worked around the problem for now but this idea looks like it
should work. Thanks - I've made a note.
 

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,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top