Click here to Skip to main content
15,891,529 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:

C++
//I have following two structure
typedef struct
{
 float rahul[6];
}RahulT;

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.
Posted
Updated 5-Mar-14 20:38pm
v2
Comments
Sergey Alexandrovich Kryukov 6-Mar-14 1:35am    
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.
—SA
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:
C++
int ii = 6;
static const tMax teststr = {
    ii,
    0,
    0};
You would get the same error!
 
Share this answer
 
Comments
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 = {
11,
&(pcRahulTvar->rahul[0]),
0
};
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:

http://stackoverflow.com/questions/12217165/const-pointer-pointing-to-a-const-pointer[^]
 
Share this answer
 

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