Structure coding and initialization

Status
Not open for further replies.

Plzhelp

Junior Member level 3
Joined
Feb 18, 2012
Messages
31
Helped
7
Reputation
14
Reaction score
6
Trophy points
1,288
Location
India
Activity points
1,535
Dear All,

I have coded a small structure and initialized it. I am getting some error while compiling this code. How can I resolve this problem?? I will be grateful for your help.

C code:
struct ver_id
{
U32 address;
U32 mode_val;
U32 rst_val;

U32 majVer : 12;
struct regDef majVer_rD;

U32 minVer : 20;
struct regDef minVer_rD ;
};

struct ver_id initval = {initval.address = 0x0, initval.rst_val = 0x00100000, initval.majVer = 1, initval.majVer_rD.maskVal=0xFFF00000,
initval.majVer_rD.mode=READ , initval.minVer = 0,initval.minVer_rD.maskVal=0x000FFFFF,initval.minVer_rD.mode=READ };


Error flashed by Visual Studio compiler:

1>c:\users\anmukher\documents\visual studio 2010\projects\structures\structures\ppcs_pma_reg.c(30): error C2099: initializer is not a constant
1>c:\users\anmukher\documents\visual studio 2010\projects\structures\structures\ppcs_pma_reg.c(30): error C2099: initializer is not a constant
1>c:\users\anmukher\documents\visual studio 2010\projects\structures\structures\ppcs_pma_reg.c(30): error C2099: initializer is not a constant
1>c:\users\anmukher\documents\visual studio 2010\projects\structures\structures\ppcs_pma_reg.c(30): error C2099: initializer is not a constant
1>c:\users\anmukher\documents\visual studio 2010\projects\structures\structures\ppcs_pma_reg.c(31): error C2099: initializer is not a constant
1>c:\users\anmukher\documents\visual studio 2010\projects\structures\structures\ppcs_pma_reg.c(31): error C2099: initializer is not a constant
1>c:\users\anmukher\documents\visual studio 2010\projects\structures\structures\ppcs_pma_reg.c(31): error C2099: initializer is not a constant
1>c:\users\anmukher\documents\visual studio 2010\projects\structures\structures\ppcs_pma_reg.c(31): error C2099: initializer is not a constant


Thanks in advance.
 

Hello,

Your problem is that you can't initialize structs like that. When initializing a struct with the {} syntax, you want to just provide a list of all the values of the members *in the order declared in the struct*. If the member is another struct, you can define the submembers using another {}. Try this (assuming that your definition of the struct regDef has the fields defined in the order maskVal, mode with no other members in the struct):

/* Edit, the forum ate my whitespace that made the comment line up with the values. Let me know if the syntax doesn't make sense... */
/* addr mode_val rst_val majVer majVer_rd maskVal mode end minVer minVer_rd maskVal mode end end_of_struct */
struct ver_id initval = {0x0, 0x0, 0x00100000, 1, { 0xFFF00000, READ }, 0, { 0x000FFFFF, READ } };

I can't run it because I don't have your definition of struct regDef, but assuming it is something like:

struct regDef {
U32 maskVal;
U32 mode;
};

Where READ is defined as some number, this should work (hopefully). Let me know...
 

struct ver_id
{
U32 address;
U32 mode_val;
U32 rst_val;

U32 majVer : 12;
struct regDef majVer_rD;

U32 minVer : 20;
struct regDef minVer_rD ;
};

in this i think you are trying to declare a structure inside another one which is not allowed
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…