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

Initialization Of Structures.

P: n/a
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
Sep 22 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On Fri, 22 Sep 2006 13:46:26 +0530, "Anand"
<an***********@in.bosch.comwrote:
>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
Sep 22 '06 #2

P: n/a
Zara wrote:
On Fri, 22 Sep 2006 13:46:26 +0530, "Anand"
<an***********@in.bosch.comwrote:

>>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.
>>The same initialization works on the Microsoft Visual Studio environment,
but fails on using Green Hills Compiler for my embedded application
programming.
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
--
Artie Gold -- Austin, Texas
http://goldsays.blogspot.com
"You can't KISS* unless you MISS**"
[*-Keep it simple, stupid. **-Make it simple, stupid.]
Sep 22 '06 #3

P: n/a
On Fri, 22 Sep 2006 13:46:26 +0530, "Anand"
<an***********@in.bosch.comwrote in comp.lang.c:
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 } };

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Sep 22 '06 #4

P: n/a
On Fri, 22 Sep 2006 10:46:16 +0200, Zara <yo****@terra.eswrote in
comp.lang.c:
On Fri, 22 Sep 2006 13:46:26 +0530, "Anand"
<an***********@in.bosch.comwrote:
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.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Sep 22 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.