为什么我会得到错误“错误:表达式必须具有常量值” [英] Why do i get error "error: expression must have a constant value"

查看:122
本文介绍了为什么我会得到错误“错误:表达式必须具有常量值”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!


我有以下代码,我在嵌入式系统中使用,

c-compiler ..但是我看到了同样的GCC的问题也是..


我需要一个地址指针的最后10位,在链接时完全知道

,但是会在编译时间。我使用

以下代码进行此初始化..

uint32 data_ptr_mask =((uint32)data)& 0x3ff;


为了说明目的,我还包括了

uint32 data_ptr_off =((uint32)数据)+ 0x3ff;


当我编译这段代码时(参见下面的例子例程)我得到

跟随gcc的错误

" error:expression必须有一个常量值"


有人可以告诉我为什么位操作符,*和/不工作

而+和 - 做。


TIA


-P.B. Srinivas

/ ******代码开头*** /

#include< stdio.h>


typedef int int32;

typedef unsigned uint32;


/ *全局变量声明** /

int32 data [64] ;


/ **这个工作** /

uint32 data_ptr =(uint32)数据;


/ **这也有用** /

uint32 data_ptr_off =((uint32)数据)+ 0x3ff;


/ **这不起作用,只有+/-运营商** /

/ **似乎有效,为什么会这样? **** /

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);


}

/ ***********代码结束************* /

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 *************/

推荐答案

在你的typedef第二个typedef语句中,你没有指定

内置类型的标识符名称。你只为这种数据类型指定了修饰符




换句话说,''unsigned''是什么?
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?


2006-03-15,Albert< al ***************** @ gmail.com>写道:
On 2006-03-15, Albert <al*****************@gmail.com> wrote:
在你的typedef第二个typedef语句中,你没有指定
内置类型的标识符名称。你只为这种数据类型指定了修饰符。

换句话说,''unsigned''是什么?
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,还有什么?


[嘿,你不会说'a''长''什么',对吗?]



int, what else?

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


2006年3月15日星期三06:13,Jordan Abel认为(

< sl ****************** ****@random.yi.org>):
On Wednesday 15 March 2006 06:13, Jordan Abel opined (in
<sl**********************@random.yi.org>):
2006-03-15,Albert< al ************** ***@gmail.com>写道:
On 2006-03-15, Albert <al*****************@gmail.com> wrote:
在你的typedef第二个typedef语句中,你没有指定
内置类型的标识符名称。你只为这种数据类型指定了修饰符。

换句话说,一个''unsigned''是什么?
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,什么别的?

[嘿,你不要说'a''长''什么',对吗?]



int, what else?

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




他一定忘记了他指的是什么,因为他没有包含任何

背景。 ;-)


-

BR,弗拉基米尔


谋杀总是一个错误 - 永远不应该做任何事情都不能用晚餐后讨论。

- 奥斯卡王尔德,多里安格雷的照片



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"


这篇关于为什么我会得到错误“错误:表达式必须具有常量值”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆