By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,030 Members | 1,997 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,030 IT Pros & Developers. It's quick & easy.

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

P: n/a
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 *************/

Mar 15 '06 #1
Share this Question
Share on Google+
10 Replies


P: n/a
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?

Mar 15 '06 #2

P: n/a
On 2006-03-15, Albert <al*****************@gmail.com> 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?


int, what else?

[hey, you don't say "a 'long' what", do you?]
Mar 15 '06 #3

P: n/a
On Wednesday 15 March 2006 06:13, Jordan Abel opined (in
<sl**********************@random.yi.org>):
On 2006-03-15, Albert <al*****************@gmail.com> 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?


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"

Mar 15 '06 #4

P: n/a
"Albert" <al*****************@gmail.com> 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) ks***@mib.org <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.
Mar 15 '06 #5

P: n/a
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

Mar 15 '06 #6

P: n/a
On Wednesday 15 March 2006 07:31, PB opined (in
<11*********************@j33g2000cwa.googlegroups. com>):
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.

Mar 15 '06 #7

P: n/a
PB

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


Mar 15 '06 #8

P: n/a
On 2006-03-15, PB <pb******@gmail.com> 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)
Mar 16 '06 #9

P: n/a

"PB" <pb******@gmail.com> wrote in message
news:11**********************@p10g2000cwp.googlegr oups.com...

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

--
RSH

Mar 17 '06 #10

P: n/a
On 14 Mar 2006 19:13:45 -0800, "PB" <pb******@gmail.com> 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.

- David.Thompson1 at worldnet.att.net
Mar 27 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.