Initialization Of Structures.

A

Anand

Greetings,

Can anyone, help me understand, why I am unable to intialize the structure
as stated below?

int normalArray[5] = {0x01, 0x02, 0x03, 0x04, 0x05};

typedef struct tag{
int value1;
int value2;
}myTypeStruct;

myTypeStruct myStrVar[2] = {
{normalArray[0],
normalArray[1]},
{normalArray[2],
normalArray[3]}
};

The compiler throws me error, stating that 'the structure intialization
requires a constant'.

The same initialization works on the Microsoft Visual Studio environment,
but fails on using Green Hills Compiler for my embedded application
programming.

Any insight, on the topic would be of great value.
Thanks for your help.

Best regards,
Anand
 
Z

Zara

Greetings,

Can anyone, help me understand, why I am unable to intialize the structure
as stated below?

int normalArray[5] = {0x01, 0x02, 0x03, 0x04, 0x05};

typedef struct tag{
int value1;
int value2;
}myTypeStruct;

myTypeStruct myStrVar[2] = {
{normalArray[0],
normalArray[1]},
{normalArray[2],
normalArray[3]}
};

The compiler throws me error, stating that 'the structure intialization
requires a constant'.

The same initialization works on the Microsoft Visual Studio environment,
but fails on using Green Hills Compiler for my embedded application
programming.

Any insight, on the topic would be of great value.
Thanks for your help.

Best regards,
Anand

Maybe, you should do what the compiler tells you:

const int normalArray[5] = {0x01, 0x02, 0x03, 0x04, 0x05};
(Test it! I am not completely sure it will work on your compiler)

regards,

Zara
 
A

Artie Gold

Zara said:
Greetings,

Can anyone, help me understand, why I am unable to intialize the structure
as stated below?

int normalArray[5] = {0x01, 0x02, 0x03, 0x04, 0x05};

That works, of course.
typedef struct tag{
int value1;
int value2;
}myTypeStruct;

myTypeStruct myStrVar[2] = {
{normalArray[0],
normalArray[1]},
{normalArray[2],
normalArray[3]}
};

The compiler throws me error, stating that 'the structure intialization
requires a constant'.
And the compiler, unsurprisingly is correct.
Note that this only applies for an initialization at file scope. Within
a function it would be correct.

Because it's an MS `extension'.
Maybe, you should do what the compiler tells you:

const int normalArray[5] = {0x01, 0x02, 0x03, 0x04, 0x05};
(Test it! I am not completely sure it will work on your compiler)

No. There is a difference between a constant and a const-qualified
object. For example, "3" is a constant. If you define:

cont int x = 3;

`x' is NOT a constant. It's just the way C works.

HTH,
--ag
 
J

Jack Klein

Greetings,

Can anyone, help me understand, why I am unable to intialize the structure
as stated below?

int normalArray[5] = {0x01, 0x02, 0x03, 0x04, 0x05};

typedef struct tag{
int value1;
int value2;
}myTypeStruct;

myTypeStruct myStrVar[2] = {
{normalArray[0],
normalArray[1]},
{normalArray[2],
normalArray[3]}
};

The compiler throws me error, stating that 'the structure intialization
requires a constant'.

The same initialization works on the Microsoft Visual Studio environment,
but fails on using Green Hills Compiler for my embedded application
programming.

Either you are using Microsoft's tool as a C++ compiler, or you are
depending on a Microsoft non-standard extension. I think the former
is more likely, I don't recall and version of Microsoft C that would
accept this when compiling C. Is there any chance that the file you
are compiling in Visual Studio has a name ending in .cpp? In that
case, it is being compiled as C++.

If these definitions are at file scope, the message the compiler is
giving you is completely correct. In C, any object with static
storage duration, which includes any objects defined at file scope,
can only be initialized with compile-time constant expressions. The
value of another object, even if it is defined with the const keyword,
is not a constant expression and cannot be used to initialize a static
object.
Any insight, on the topic would be of great value.
Thanks for your help.

Define your values in macros (not so good), or if they are all in the
range of values that fits into an int, an enumeration (much better):

enum { val_0 = 0x01, val_1 = 0x02, val_2 = 0x03, val_3 = 0x04, val_4 =
0x05 };

int normalArray[5] = { val_0, val_1, val_2, val_3, val_4 };

struct myname [2] = { { val_0, val_1 }, { val_2, val_3 } };
 
J

Jack Klein

Greetings,

Can anyone, help me understand, why I am unable to intialize the structure
as stated below?

int normalArray[5] = {0x01, 0x02, 0x03, 0x04, 0x05};

typedef struct tag{
int value1;
int value2;
}myTypeStruct;

myTypeStruct myStrVar[2] = {
{normalArray[0],
normalArray[1]},
{normalArray[2],
normalArray[3]}
};

The compiler throws me error, stating that 'the structure intialization
requires a constant'.

The same initialization works on the Microsoft Visual Studio environment,
but fails on using Green Hills Compiler for my embedded application
programming.

Any insight, on the topic would be of great value.
Thanks for your help.

Best regards,
Anand

Maybe, you should do what the compiler tells you:

const int normalArray[5] = {0x01, 0x02, 0x03, 0x04, 0x05};
(Test it! I am not completely sure it will work on your compiler)

Completely dead wrong. It certainly won't if it is a conforming C
compiler.
 

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,998
Messages
2,570,242
Members
46,834
Latest member
vina0631

Latest Threads

Top