beginner question

M

Michael

Thanks to everyone who responded on my last question. I have come
across something I don't understand, I had a good look through the FAQ
first, and I didn't find anything about it.


I have an array of global constants:

const unsigned long unit_id[8] = {
0x00323130,
0x00000000,
0x00000000,
0x00000000,
0x00000000,
0x00000000,
0x00000000,
0x00000000,
};

I want to initialise a static variable in a function using
'unit_id[0]'

{
static unsigned long shifter=unit_id[0];

This gives me an error 'non-address/-constant initializer'
I am using the Keil C51 compiler V7.10 for 8051 based processors.
 
B

Ben Pfaff

I have an array of global constants:

const unsigned long unit_id[8] = {
0x00323130,
[...]

};

I want to initialise a static variable in a function using
'unit_id[0]'

{
static unsigned long shifter=unit_id[0];

The values of const objects can't be used in constant
expressions. Instead, create a macro that expands to this value
and use the macro in both the definition of the array and the
static variable.
 
R

Russell Hanneken

Michael said:
I have an array of global constants:

const unsigned long unit_id[8] = {
0x00323130,
0x00000000,
0x00000000,
0x00000000,
0x00000000,
0x00000000,
0x00000000,
0x00000000,
};

This could be shortened to

const unsigned long uit_id[8] = { 0x00323130 };

If an array initializer list has fewer initializers than the array has
elements, the remaining elements in the array are set to 0.
I want to initialise a static variable in a function using
'unit_id[0]'

{
static unsigned long shifter=unit_id[0];

This gives me an error 'non-address/-constant initializer'

Objects of static storage duration have to be initialized with a
"constant expression," which in C basically means an expression whose
operands are all constants (0 is, but unit_id isn't, despite the fact
that it stores unsigned long values that can't be modified).
 
M

Michael

Russell Hanneken said:
Michael said:
I have an array of global constants:

const unsigned long unit_id[8] = {
0x00323130,
0x00000000,
0x00000000,
0x00000000,
0x00000000,
0x00000000,
0x00000000,
0x00000000,
};

This could be shortened to

const unsigned long uit_id[8] = { 0x00323130 };

If an array initializer list has fewer initializers than the array has
elements, the remaining elements in the array are set to 0.
I want to initialise a static variable in a function using
'unit_id[0]'

{
static unsigned long shifter=unit_id[0];

This gives me an error 'non-address/-constant initializer'

Objects of static storage duration have to be initialized with a
"constant expression," which in C basically means an expression whose
operands are all constants (0 is, but unit_id isn't, despite the fact
that it stores unsigned long values that can't be modified).

Interesting. This array of constants will be modified in the E^2 rom
that the micro boots from. Therefore the macro idea won't work.
I think my only alternative is to declare ‘shifter' as a global
variable, and initialize it in my init() function.

Thanks again.

Regards, Michael.
 
S

Sten Westerback

Michael said:
Russell Hanneken <[email protected]> wrote in message
Michael said:
I have an array of global constants:

const unsigned long unit_id[8] = {
0x00323130,
0x00000000,
0x00000000,
0x00000000,
0x00000000,
0x00000000,
0x00000000,
0x00000000,
};

This could be shortened to

const unsigned long uit_id[8] = { 0x00323130 };

If an array initializer list has fewer initializers than the array has
elements, the remaining elements in the array are set to 0.
I want to initialise a static variable in a function using
'unit_id[0]'

{
static unsigned long shifter=unit_id[0];

This gives me an error 'non-address/-constant initializer'

Objects of static storage duration have to be initialized with a
"constant expression," which in C basically means an expression whose
operands are all constants (0 is, but unit_id isn't, despite the fact
that it stores unsigned long values that can't be modified).

Interesting. This array of constants will be modified in the E^2 rom
that the micro boots from. Therefore the macro idea won't work.
I think my only alternative is to declare 'shifter' as a global
variable, and initialize it in my init() function.

If for instance -1 (or some other flag value) is an invalid value in your
case you could use that as the initialisation and then inside the
functions code you do
if (shifter==-1) shifter = unit_id[0];

- Sten
 

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

No members online now.

Forum statistics

Threads
474,141
Messages
2,570,817
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top