473,399 Members | 3,888 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,399 software developers and data experts.

When using a const struct field in an inialization I get an error

Hi,

Hi, I have a typedefed struct, and later when I declare multiple const structures and want to use a field of one in an inialization, I get the following error: "error C2099: initializer is not a constant".

typedef struct {
int Start;
int attribute;
} MyStructure ;

#define FIRST 1

const MyStructure STRUCT0={FIRST, 0} ;
const MyStructure STRUCT1={STRUCT0.Start+5, 25} ;
const MyStructure STRUCT2={STRUCT1.Start+35, 175} ;

Any idea about to solve this? It's a c program, and I'm compiling it under Visual Studio 2008.

I don't want to remove the "#define" because it allows me to change multiple structures at once and is more clear conceptually (actuall sometimes FIRST can be a different number), and also I don't want to use the definition in the second, third and following structs declarations because I have multiple defines that are used, and the expressions will become huge as I have more than 20 of this types of structs.

TIA & Regards ...
Feb 19 '08 #1
4 1540
weaknessforcats
9,208 Expert Mod 8TB
Compiles and links using Visual Studio.NET 2005.

Expand|Select|Wrap|Line Numbers
  1. #include "stdafx.h"
  2. typedef struct {
  3. int Start;
  4. int attribute;
  5. } MyStructure ;
  6.  
  7. #define FIRST 1
  8.  
  9. const MyStructure STRUCT0={FIRST, 0} ;
  10. const MyStructure STRUCT1={STRUCT0.Start+5, 25} ;
  11. const MyStructure STRUCT2={STRUCT1.Start+35, 175} ;
  12. int main()
  13. {
  14.  
  15. }
  16.  
Feb 19 '08 #2
Compiles and links using Visual Studio.NET 2005.

Expand|Select|Wrap|Line Numbers
  1. #include "stdafx.h"
  2. typedef struct {
  3. int Start;
  4. int attribute;
  5. } MyStructure ;
  6.  
  7. #define FIRST 1
  8.  
  9. const MyStructure STRUCT0={FIRST, 0} ;
  10. const MyStructure STRUCT1={STRUCT0.Start+5, 25} ;
  11. const MyStructure STRUCT2={STRUCT1.Start+35, 175} ;
  12. int main()
  13. {
  14.  
  15. }
  16.  
It also compiles as a cpp file under Visual Studio 2008, but as my whole program is a C program, I didn't wanted it to be restricted to be a C++ program (i.e. to not compile under a pure C compiler).

So, any aditional idea?

TIA & regards ...
Feb 19 '08 #3
weaknessforcats
9,208 Expert Mod 8TB
Then you have to write C.

C++ is not C so C++ code often does not translate to C. Also, some C syntax and rules are C++ compile errors. You can't have it both ways.

However, the C solution is:
Expand|Select|Wrap|Line Numbers
  1. typedef struct {
  2. int Start;
  3. int attribute;
  4. } MyStructure ;
  5.  
  6. #define FIRST 1
  7.  
  8.  
  9.  
  10. int main()
  11. {
  12. const MyStructure STRUCT0={FIRST, 0} ;
  13. const MyStructure STRUCT1={STRUCT0.Start+5, 25} ;
  14. const MyStructure STRUCT2={STRUCT1.Start+35, 175} ;
  15.  
  16. }
  17.  
You have to initialize your struct variables inside main().

Doing it outside of main() requires a constructor, which is not present in C. Since you don't have a constructor, the compiler provides one that initializes each member of the struct using the values between the braces. If you change your struct to a class and make the members public, it will still compile in C++.
Feb 20 '08 #4
Then you have to write C.

C++ is not C so C++ code often does not translate to C. Also, some C syntax and rules are C++ compile errors. You can't have it both ways.

However, the C solution is:
Expand|Select|Wrap|Line Numbers
  1. typedef struct {
  2. int Start;
  3. int attribute;
  4. } MyStructure ;
  5.  
  6. #define FIRST 1
  7.  
  8.  
  9.  
  10. int main()
  11. {
  12. const MyStructure STRUCT0={FIRST, 0} ;
  13. const MyStructure STRUCT1={STRUCT0.Start+5, 25} ;
  14. const MyStructure STRUCT2={STRUCT1.Start+35, 175} ;
  15.  
  16. }
  17.  
You have to initialize your struct variables inside main().

Doing it outside of main() requires a constructor, which is not present in C. Since you don't have a constructor, the compiler provides one that initializes each member of the struct using the values between the braces. If you change your struct to a class and make the members public, it will still compile in C++.
Thanks for the suggestions weaknessforcats, this has two problems also, but also gave me an idea.

One of the problems is that these constants are used everywhere, so they either have to have global visibility or be passed everywhere as a parameter. And the second one is that I also have to super-optimize my program for speed, so passing them as a parameter is a negative, and also the fact that they have to be initialized dinamically and so these prevents a lot of constants calculations that is made with them to be done in the compilation.

I can try to declare them as global variables (not constants), and see how much is the performance hit I get.

For now I'm using (and perhaps abusing) of the ## operator, and have tons of constants like this:
const MyStructure STRUCT0={FIRST, 0} ;
const MyStructure STRUCT1={STRUCT0.Start+5, 25} ;
const MyStructure STRUCT2={STRUCT1.Start+35, 175} ;

#define STRUCT0_START FIRST
#define STRUCT0_ATTRIBUTE 0
#define STRUCT1_START (STRUCT0_START+5)
#define STRUCT1_ATTRIBUTE 25
#define STRUCT2_START (STRUCT1_START+35)
#define STRUCT2_ATTRIBUTE 175

And then I access them by:
#define MyStructGetStart(number) STRUCT##number##_START
#define MyStructGetAttribute(number) STRUCT##number##_ATTRIBUTE

I'll see if I can come with something better, any additional idea is appreciated.

TIA & Regards ...
Feb 21 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: jeff regoord | last post by:
A user inputs a float value. The scanf() function gets the value. However, I need to create an error handler with an if else statement saying invalid input if the input is not a number. Does...
9
by: Skybuck Flying | last post by:
Hello, What does Const mean in this c structure ? and what is the delphi equivalent ? I think const struct just means it can't be modified... is that correct ? Struct { Type1 Field1;...
4
by: QQ | last post by:
Hello I am a newbie on network programming. I am trying to receive a packet if((numbytes = recvfrom(udp_fd1, buf, MAXLEN-1, 0,(struct sockaddr*)&register_addr, &addr_len))==-1){ fprintf(stderr,...
4
by: Aaron Queenan | last post by:
When I build a C++ library to .NET using the managed C++ compiler, I get the following error message: Linking... LINK : error LNK2020: unresolved token (0A000005) _CrtDbgReport LINK : error...
0
by: Sushma | last post by:
Hi All I'm porting my application from VC6 to VC7. There is a COM exe that has successfully registered itself with system. But when I'm using its services in other Dll I get this error. ...
13
by: kamaraj80 | last post by:
Hi I am using the std:: map as following. typedef struct _SeatRowCols { long nSeatRow; unsigned char ucSeatLetter; }SeatRowCols; typedef struct _NetData
9
by: weirdwoolly | last post by:
Hopefully someone will be able to help. I have written a stored procedure in C++ called from a Java test harness to validate the graphic data types in C++ and their use. I have declared the...
2
by: pvl_google | last post by:
Hi, I'm trying to extend an STL class with additional iterator functionality. In the simplified example below I added an extra iterator class with a dereferencing operator. This operator...
0
by: raylopez99 | last post by:
I ran afoul of this Compiler error CS1612 recently, when trying to modify a Point, which I had made have a property. It's pointless to do this (initially it will compile, but you'll run into...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.