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

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.

Advertisements

On 2006-03-15, Albert <> wrote:
> 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?

"Albert" <> writes:
> 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.)

--
Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.

On Wednesday 15 March 2006 07:31, PB opined (in
<>):
> 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.

--
BR, Vladimir

In 1750 Issac Newton became discouraged when he fell up a flight of
stairs.

Vladimir S. Oka wrote:
> On Wednesday 15 March 2006 07:31, PB opined (in
> <>):
>
> > 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.
>

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 "&".
> --
> BR, Vladimir
>
> In 1750 Issac Newton became discouraged when he fell up a flight of
> stairs.

On 2006-03-15, PB <> wrote:
> 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.

--
- David A. Holland
(the above address works if unscrambled but isn't checked often)

"PB" <> wrote in message
news:...
>
> Vladimir S. Oka wrote:
> > On Wednesday 15 March 2006 07:31, PB opined (in
> > <>):
> >
> > > 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.
> >
>
> 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.

On 14 Mar 2006 19:13:45 -0800, "PB" <> wrote:
> 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.

Share This Page

Welcome to The Coding Forums!

Welcome to the Coding Forums, the place to chat about anything related to programming and coding languages.

Please join our friendly community by clicking the button below - it only takes a few seconds and is totally free. You'll be able to ask questions about coding or chat with the community and help others.
Sign up now!