473,395 Members | 1,986 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Initialization Of Structures.

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
4 3571
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: JKop | last post by:
What's the difference between them? Take the following: #include <iostream> struct Blah { int k;
6
by: Neil Zanella | last post by:
Hello, I would like to know whether the following C fragment is legal in standard C and behaves as intended under conforming implementations... union foo { char c; double d; };
2
by: leatherbrain | last post by:
Is there any way I can initialize the value of a single member of a structure in the declaration of an array of these structures? Eg. struct StructEx { int a; int b; }
4
by: Theo R. | last post by:
Hi all, I have the following struct defined - #define INTEGER 0 #define STRING 1 typedef struct { char type ; union {
6
by: Daniel Rudy | last post by:
Hello Group. Please consider the following code: /* this table is used in the wipedevice routine */ static const struct wipe_t { uchar wte; /* wipe table entry */ } wipetable = {...
8
by: Sheldon | last post by:
Hi, Can anyone help with this problem with setting up nested structures and initializing them for use. I have created several structs and placed them in a super struct that I will then pass to...
17
by: copx | last post by:
I don't know what to think of the following.. (from the dietlibc FAQ) Q: I see lots of uninitialized variables, like "static int foo;". What gives? A: "static" global variables are initialized...
17
by: jb.simon | last post by:
Recently I was pinged in a code review about my use of the initialization method AStruct myStruct = { 0 } ; Which initializes all elements of the myStruct to 0. I was questioned on it because...
17
by: Andrea Taverna (Tavs) | last post by:
Subject: Initialization of a const matrix implemented as pointer-to-pointer Hello everyone. I've got the following matrix definition in a source file static const char **a; I need it to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.