Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Why do i get error "error: expression must have a constant value"

Reply
Thread Tools

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

 
 
PB
Guest
Posts: n/a
 
      03-15-2006
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 *************/

 
Reply With Quote
 
 
 
 
Albert
Guest
Posts: n/a
 
      03-15-2006
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?

 
Reply With Quote
 
 
 
 
Jordan Abel
Guest
Posts: n/a
 
      03-15-2006
On 2006-03-15, Albert <(E-Mail Removed)> 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?]
 
Reply With Quote
 
Vladimir S. Oka
Guest
Posts: n/a
 
      03-15-2006
On Wednesday 15 March 2006 06:13, Jordan Abel opined (in
<(E-Mail Removed)>):

> On 2006-03-15, Albert <(E-Mail Removed)> 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"

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      03-15-2006
"Albert" <(E-Mail Removed)> 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.velocityreviews.com/forums/(E-Mail Removed) <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.
 
Reply With Quote
 
PB
Guest
Posts: n/a
 
      03-15-2006
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

 
Reply With Quote
 
Vladimir S. Oka
Guest
Posts: n/a
 
      03-15-2006
On Wednesday 15 March 2006 07:31, PB opined (in
<(E-Mail Removed). 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.

 
Reply With Quote
 
PB
Guest
Posts: n/a
 
      03-15-2006

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


 
Reply With Quote
 
David Holland
Guest
Posts: n/a
 
      03-16-2006
On 2006-03-15, PB <(E-Mail Removed)> 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)
 
Reply With Quote
 
Robin Haigh
Guest
Posts: n/a
 
      03-17-2006

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



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" Adem C++ 42 11-04-2008 12:39 PM
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" Adem C Programming 45 11-04-2008 12:39 PM
Case expression must be constant expression Philipp Java 26 11-25-2007 10:10 PM
"error C2057: expected constant expression", "error C2466: cannot allocate an array of constant size 0". Why doesn't my simple program work??? hn.ft.pris@gmail.com C++ 13 01-22-2007 02:03 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM



Advertisments