Click here to Skip to main content
15,071,262 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi All
can anyone please tell me how to initialize an array at compile time using preprocessor.
Above is the aternative That I thought. My actual problem is as below:

//I have following two structure
typedef struct
 float rahul[6];

typedef struct Max{					
   int ax;
   const float* f1;
   const float* f2;
} tMax;

//intitialze the structure and declare a pointer to it.
const RahulT  RahulTvar={{3,4,6,8,9,7}};
const RahulT*  pRahulTvar = &RahulTvar;

//using the structures's array in second static const str.
static const tMax teststr = {
   11 /* Nx:  */, 
  &(pRahulTvar->rahul[0]),//this is giving error:  error C2099: initializer is not a constant
  &(RahulTvar.rahul[0])// it is working properly

Why is the problem is there. and what is the possible solution.
I want to use pointer option to save temporary memory copy operation. But in case of pointer it is giving problem.
Updated 5-Mar-14 20:38pm
Not clear or not correct. Preprocessor does not define or initialize anything, it substitutes one string with another, in source code.
What are you trying to achieve.
Rahul@puna 6-Mar-14 2:39am
now have a look at question and suggest me proper solution or workaround.
thanks in advance

The error message is kinda explicit: "initializer is not a constant" - which it isn't.

The address of an object is not a constant value as far as the compiler is concerned, because it is defined by the linker which locates the program and its data inside the memory space. The compiler doesn't - if defines everything in terms of it's size, and lets the linker sort out the actual address.

But you can't use the value of any variable as a constant value under any circumstances - which is what you are trying to do.
Even if you did this:
int ii = 6;
static const tMax teststr = {
You would get the same error!
Rahul@puna 6-Mar-14 3:26am
I got your point that value of any variable as a constant value under any circumstances. But my problem is that we accessing the address in both cases as shown below:
1. &(pRahulTvar->rahul[0]),//this is giving error: error C2099: initializer is not a constant
2. &(RahulTvar.rahul[0])// it is working properly
And 2nd is working properly, only 1 is giving error and why, I am still not clear. Could u please look into this and elaborate ur answer in reference with the above two lines.
Thanks in advance.
OriginalGriff 6-Mar-14 4:28am
No, you aren't.
In the first case, you are accessing a variable via the -> operator.
Stefan_Lang 6-Mar-14 4:51am
Actually he's accessing a *presumably* const value through a _variable_ pointer. The point being that the pointer could be reassigned to something that is *not* const.
Stefan_Lang 6-Mar-14 9:35am
Ignore my previous reply. Apparently my statement only applies to C++, not C. However, I've found another explanation and a workaround at SO. (see the link in my solution if you're interested)
Stefan_Lang 6-Mar-14 4:44am
In the first case, pRahulTvar is not constant, it is a (variable) pointer to a constant array. you could try this definition instead:
const RahulT* const pcRahulTvar = &RahulTvar;
static const tMax teststr = {
Rahul@puna 6-Mar-14 4:50am
I have also tried the above code but it is also not working
Stefan_Lang 6-Mar-14 4:54am
For me it works. Please note that I changed the pointer name to a prefix of "pc" to indicate "const pointer".
Rahul@puna 6-Mar-14 5:46am
Ok I will once again check it. and note that the code is in C not c++.
Rahul@puna 6-Mar-14 5:56am
The problem persist, please check it on c compiler
Stefan_Lang 6-Mar-14 7:07am
That's odd, I am not aware of anything in my suggestion that is C++ specific. But you are correct, it doesn't compile as C. I will need to look into this more closely. IMHO it should compile in C as well.
Rahul@puna 6-Mar-14 7:35am
yes please. have a close look at it. as it will solve my very important problem. Thanks
I couldn't quite understand why the suggestion I made works in C++, but not in C. So I checked to see if others came across the same problem. Indeed I found an entry on SO, and the second response offers a halfway understandable explanation as well as a workaround that might help in your situation:[^]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900