473,586 Members | 2,472 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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
10 21144
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
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
On Wednesday 15 March 2006 06:13, Jordan Abel opined (in
<sl************ **********@rand om.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
"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_Keit h) 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
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
On Wednesday 15 March 2006 07:31, PB opined (in
<11************ *********@j33g2 000cwa.googlegr oups.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
PB

Vladimir S. Oka wrote:
On Wednesday 15 March 2006 07:31, PB opined (in
<11************ *********@j33g2 000cwa.googlegr oups.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
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

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

Vladimir S. Oka wrote:
On Wednesday 15 March 2006 07:31, PB opined (in
<11************ *********@j33g2 000cwa.googlegr oups.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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
4381
by: asdf | last post by:
Hello, I was enjoying working in VS for half a year without any problems and now I cannot debug anymore. Without any really reason my Studio tells me that the page that I want to debug has - No symbols loaded. I use VS v1.7.3088 and .NET framework v1.1.4322 SP1. I work with ASP.NET using VB and I'm trying to set a breakpoint on the...
2
14241
by: drisso | last post by:
Hello, I have searched that error on these message boards, msdn and crystaldecisions.com. I found tutorials that showed how to export a crystal report to pdf, but like many others I am getting an error when I actually do the oRpt.Export() command. The error is "Missing parameter field current value." I looked this error up on...
5
8072
by: Dmitriy Lapshin [C# / .NET MVP] | last post by:
Hi all, I think the VB .NET compiler should at least issue a warning when a function does not return value. C# and C++ compilers treat this situation as an error and I believe this is the right thing to do. And I wonder why VB .NET keeps silence and makes such function return some default value instead. Isn't it error-prone? -- Dmitriy...
8
21603
by: Steve Kershaw | last post by:
I have a debugging/breakpoint problem that I must fix before I move on!!! When I set a breakpoint in my ASP.NET (C#) page I get the red circle with the 'A'. When I mouse over this breakpoint I get one of two errors: First error "The breakpoint will not currently be hit. No symbols have been loaded for this document"
3
9445
by: _Christopher\(M2M\) | last post by:
Microsoft Visual Studio 2005 Version 8.0.50727.762 (SP.050727-7600) How do I get rid of "Warning C4307: '-' : integral constant overflow" from the following macro? #define d_ReturnMax(I_iTypeSize)\ ( (int64(1)<<((I_iTypeSize*8)-1))-1) There NO warnings when done as a function: int64 d_ReturnMax(int64 I_iTypeSize) { return...
0
7915
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8204
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7965
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6617
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5712
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5392
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3838
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2345
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1452
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.