Why do i get error "error: expression must have a constant value"

P

PB

Hi !

I have the following code, which I am using in an Embedded systems,
c-compiler.. However I see the same problem with GCC too..

I need the last 10 bits of an address pointer, which is completly know
at link time, but will be symbols at the compile time. I used the
following code for this initalization..
uint32 data_ptr_mask = ((uint32) data) & 0x3ff;

for illustration purpose, I have also included
uint32 data_ptr_off = ((uint32 ) data) +0x3ff;

When I compile this code (see the example routine below) I get the
following error with gcc
"error: expression must have a constant value"

Can somebody tell me why the bit operators, "*" and "/" don't work
while "+" and "-" do.

TIA

-P.B. Srinivas
/****** Start of CODE ***/
#include <stdio.h>

typedef int int32;
typedef unsigned uint32;

/* Global variable declarations **/
int32 data[64];

/** this works **/
uint32 data_ptr = (uint32 ) data;

/** this also works **/
uint32 data_ptr_off = ((uint32 ) data) +0x3ff;

/** this doesn't work , only +/- operators **/
/** seem to work, why is this ? ****/
uint32 data_ptr_mask = ((const uint32) data) & 0x3ff;

int main()
{
printf("Pointer Addition %x %x\n",data_ptr, data_ptr_off);
printf("Pointer Masking %x %x\n",data_ptr, data_ptr_mask);

}
/*********** End of code *************/
 
A

Albert

In your typedef second typedef statement you have not specified the
built in-type's identifier name. You have only specified the modifier
for that data type.

In other words, an 'unsigned' what?
 
J

Jordan Abel

In your typedef second typedef statement you have not specified the
built in-type's identifier name. You have only specified the modifier
for that data type.

In other words, an 'unsigned' what?

int, what else?

[hey, you don't say "a 'long' what", do you?]
 
V

Vladimir S. Oka

In your typedef second typedef statement you have not specified the
built in-type's identifier name. You have only specified the modifier
for that data type.

In other words, an 'unsigned' what?

int, what else?

[hey, you don't say "a 'long' what", do you?]

He must have forgot what he was referring to as he did not include any
context. ;-)

--
BR, Vladimir

Murder is always a mistake -- one should never do anything one cannot
talk about after dinner.
-- Oscar Wilde, "The Picture of Dorian Gray"
 
K

Keith Thompson

Albert said:
In your typedef second typedef statement you have not specified the
built in-type's identifier name. You have only specified the modifier
for that data type.

In other words, an 'unsigned' what?

Please provide context. Read <http://cfaj.freeshell.org/google/> to
understand how and why.

The type "unsigned int" can also be referred to as "unsigned".

The declaration you're complaining about is:

typedef unsigned uint32;

which is perfectly legal. (It does implictly assume that unsigned int
is 32 bits, but that's a different issue.)
 
P

PB

sorry it is supposed to be unsigned int.. the compiler automatically
converts unsigned to unsigned int..

I see the same error even if I put "int" after unsigned
 
V

Vladimir S. Oka

sorry it is supposed to be unsigned int.. the compiler automatically
converts unsigned to unsigned int..

I see the same error even if I put "int" after unsigned

Please provide context. See <http://cfaj.freeshell.org/google/>. This
one <http://clc-wiki.net/wiki/Introduction_to_comp.lang.c> is also
useful.

As others have pointed out, `unsigned` is exactly the same as `unsigned
int`. There's no error on your part there. Your problem is using
non-constant initialisers for your variables. I'd recommend re-writing
your code to avoid this. Consider moving such stuff into a function.
 
P

PB

Vladimir said:
Please provide context. See <http://cfaj.freeshell.org/google/>. This
one <http://clc-wiki.net/wiki/Introduction_to_comp.lang.c> is also
useful.

As others have pointed out, `unsigned` is exactly the same as `unsigned
int`. There's no error on your part there. Your problem is using
non-constant initialisers for your variables. I'd recommend re-writing
your code to avoid this. Consider moving such stuff into a function.

thanks for all the replies.. since I am working on a embedded system, i
really don't like putting in a function for initialization, as it is
take up code memory.. I have only posted an example of the problem, i
have dozen or more of these initilization "macros' in the code..

I find it difficult to understand why the compiler/linker cant do this
initialization, as these are really not "non-constant" but fixed
initializers that are resolved at compile time.. also if the "+"
operator works why not "&".
 
D

David Holland

> thanks for all the replies.. since I am working on a embedded system, i
> really don't like putting in a function for initialization, as it is
> take up code memory.. I have only posted an example of the problem, i
> have dozen or more of these initilization "macros' in the code..
>
> I find it difficult to understand why the compiler/linker cant do this
> initialization, as these are really not "non-constant" but fixed
> initializers that are resolved at compile time.. also if the "+"
> operator works why not "&".

It might be able to, but the C standard says it doesn't have to and/or
shouldn't.

Don't forget that you can write a program (to run on your development
machine) to generate a source file that has all the precomputed
initialization you want. Then you can compile this source file and
link it into the program that runs on your embedded target.

Just be careful to make sure this program does these computations the
same way your target does, or you can end up with subtle and bizarre
problems.
 
R

Robin Haigh

PB said:
thanks for all the replies.. since I am working on a embedded system, i
really don't like putting in a function for initialization, as it is
take up code memory.. I have only posted an example of the problem, i
have dozen or more of these initilization "macros' in the code..

I find it difficult to understand why the compiler/linker cant do this
initialization, as these are really not "non-constant" but fixed
initializers that are resolved at compile time.. also if the "+"
operator works why not "&".

Because as you said originally, the array address is a symbol at compile
time, so address-based initializers have to be resolved in the linker.

The linker is language-independent, it doesn't know C, it can't evaluate C
expressions according to C rules. It can add offsets to addresses. So, in
C, there are different kinds of constant expression. An arithmetic constant
expression is computed in the compiler, it can do most kinds of operations,
but the operands have to be available to the compiler as numbers. An
address constant can be computed in the linker, can have an address as an
operand, but all you can do with it is add a constant offset.

Casting a pointer to an integer type isn't supported by the standard in any
type of static initializer. Your compilers let you get away with it only
when the expression is similar to a legal address constant and they can
compile it into something linkable.
 
D

Dave Thompson

Hi !

I have the following code, which I am using in an Embedded systems,
c-compiler.. However I see the same problem with GCC too..

I need the last 10 bits of an address pointer, which is completly know
at link time, but will be symbols at the compile time. I used the

Right. In general this is and only can be determined at link time.
following code for this initalization..
uint32 data_ptr_mask = ((uint32) data) & 0x3ff;

for illustration purpose, I have also included
uint32 data_ptr_off = ((uint32 ) data) +0x3ff;

When I compile this code (see the example routine below) I get the
following error with gcc
"error: expression must have a constant value"

Can somebody tell me why the bit operators, "*" and "/" don't work
while "+" and "-" do.
Because 6.6p7 allows "an address constant for an object type plus or
minus an integer constant" <OT> in turn because linkers traditionally
support that but not portably other operations like your bitwise. </>
(Actually what you wrote isn't strictly an address constant but rather
the conversion of an address constant; however, on most systems,
including the ones gcc supports, this is effectively an identity.)

<OT> Depending on your linker, you may be able to cause (all or
specific) data to aligned at a 10-bit boundary, or even placed at a
fixed location; in either case the offset is known in advance and you
can compile it as a constant. </> Otherwise you'll have to do this
computation at run time, probably early in your initialization code.

- David.Thompson1 at worldnet.att.net
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top