const数组声明 [英] const array declaration

查看:98
本文介绍了const数组声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


在一个程序(不是我自己的)中,我遇到了一个指向一个由另外两个const组成的数组的常量

指针的声明数组的指针。

不太确定他们为什么这么复杂,但是合法吗?大多数

编译器接受它,但是没有人认识到rhs是常数。

在声明const的时候rhs的要求是什么? >
指针?以下程序是否合法C?


int main(int argc,char * argv []){

const char * a [] = {" A"};

const char * b [] = {" B"};

const char ** z [] = {a,b}; / *这是有问题的陈述* /

return(0);

}


错误信息是

" t.c",第4行:错误:初始化:预期常量表达式

变量:`z''


提前致谢,


赫伯特

Hi guys,

In a program (not my own) I encountered the declaration of a constant
pointer to an array consisting of two other const pointers to arrays.
Not quite sure why they do it so complicated, but is it legal? Most
compilers accept it, but one doesn''t recognize the rhs as a constant.
What are the requirements for the rhs in the declaration of a const
pointer? Is the following program legal C?

int main(int argc, char *argv[]) {
const char *a[] = {"A"};
const char *b[] = {"B"};
const char **z[] = {a, b}; /* this is the statement in question */
return (0);
}

The error message is
"t.c", line 4: error: initialization: constant expression is expected
for variable: `z''

Thanks in advance,

Herbert

推荐答案



" herbertF" ; < HF ****** @ yahoo.co.uk>在消息中写道

新闻:8d ************************** @ posting.google.c om ...

"herbertF" <hf******@yahoo.co.uk> wrote in message
news:8d**************************@posting.google.c om...
大家好,

在一个程序(不是我自己的)中,我遇到了一个指向一个数组的常量指针的声明,该数组包含两个其他指向数组的const指针。
不太确定他们为什么这么复杂,但是合法吗?大多数
编译器接受它,但是没有人认识到rhs是一个常量。
在指定const
指针时rhs的要求是什么?以下程序是合法的C吗?
int main(int argc,char * argv []){
const char * a [] = {" A"};
const char * b [] = {" B"};
const char ** z [] = {a,b}; / *这是有问题的陈述* /
return(0);
}
Hi guys,

In a program (not my own) I encountered the declaration of a constant
pointer to an array consisting of two other const pointers to arrays.
Not quite sure why they do it so complicated, but is it legal? Most
compilers accept it, but one doesn''t recognize the rhs as a constant.
What are the requirements for the rhs in the declaration of a const
pointer? Is the following program legal C?

int main(int argc, char *argv[]) {
const char *a[] = {"A"};
const char *b[] = {"B"};
const char **z[] = {a, b}; /* this is the statement in question */
return (0);
}




你的const在错误的位置。 />

int main(int argc,char * argv []){

char const * a [] = {" A"};

char const * b [] = {" B"};

char const ** z [] = {a,b}; / *这是有问题的陈述* /

返回(0);

}


工作正常。


Tom



Your const was in the wrong spot.

int main(int argc, char *argv[]) {
char const *a[] = {"A"};
char const *b[] = {"B"};
char const **z[] = {a, b}; /* this is the statement in question */
return (0);
}

Works just fine.

Tom


herbertF写道:
herbertF wrote:

大家好,
指针的声明,该数组由另外两个指向数组的const指针组成。
不太确定为什么它们如此复杂,但是合法吗?大多数
编译器接受它,但是没有人认识到rhs是一个常量。
在指定const
指针时rhs的要求是什么?以下程序是合法的C吗?
int main(int argc,char * argv []){
const char * a [] = {" A"};
const char * b [] = {" B"};
const char ** z [] = {a,b}; / *这是有问题的陈述* /
return(0);
}

错误信息是
t.c,第4行:错误:初始化:期望常量表达式
变量:`z''

Hi guys,

In a program (not my own) I encountered the declaration of a constant
pointer to an array consisting of two other const pointers to arrays.
Not quite sure why they do it so complicated, but is it legal? Most
compilers accept it, but one doesn''t recognize the rhs as a constant.
What are the requirements for the rhs in the declaration of a const
pointer? Is the following program legal C?

int main(int argc, char *argv[]) {
const char *a[] = {"A"};
const char *b[] = {"B"};
const char **z[] = {a, b}; /* this is the statement in question */
return (0);
}

The error message is
"t.c", line 4: error: initialization: constant expression is expected
for variable: `z''




抱怨的编译器是正确的:a和b是不是

常量表达式。

尽管有拼写,但'const''不是

常量。


如果你在更广泛的背景下考虑它,你会明白为什么

`a''和'b''不是常数。这是一个具有类似结构的递归函数

,以帮助显示正在发生的事情:


void func(int x){

const char * a [] = {" A"," B" };

if(0< = x&& x< 2){

a [x] =" X";

printf(" func(%d):a [0] =%s,a [1] =%s \ n",

x,a [0],a [1] );

func(x + 1);

printf(" func(%d):a [0] =%s,a [1] =%s \ n",

x,a [0],a [1]);

}

else {

printf(" func(%d):无所事事\ nn",x);

}

}


如果用`func(0)''调用这个函数,输出将是


func(0):a [0] = X,a [1] = B

func(1):a [0] = A,a [1] = X

func(2):无事可做

func(1):a [0] = A,a [1] = X

func(0):a [0] = X,a [1] = B


这表明存在两个不同的a数组:一个在外部func(0)调用中,另一个在内部函数(1)中。另外一个在内部函数(1)中。

你可以看到,当外部函数(0)执行a [0] =" X时,必须有多个`a []''因为

''内部函数(1)中'a [0]''

的值不受影响;同样当内部

func(1)设置'a [1] =" X"''它不会改变

中的'a [1]''外部函数(0)。每个`a []''数组在其func()的
调用开始时就会存在,并且当它自己的
func()返回时它就不再存在。因此,即使'a''只是一个标识符,

它在不同的时间指定不同的数组对象,因此
不是常数。


如果您更改了将数组声明为


static const char * a [] = {" A"," B" };


你会得到一个不同的输出:


func(0):a [0] = X,a [1] = B

func(1):a [0] = X,a [1] = X

func(2):无事可做

func(1):a [0] = X,a [1] = X

func(0):a [0] = X,a [1] = X


在这种情况下,只有一个`a []''数组,并且在另一个func()调用中对它进行的任何更改

都可以看到。这个单一的'a []''的
生命周期不再与其包含块的执行

相关联;这个'a []''在
之前出现,程序开始执行并继续存在,直到

程序终止。在这种情况下,'a''*是*常量,因为

标识符指的是

程序运行的整个时间内只有一个对象。如果你的原始程序只使用一对一的语义

,也许可以通过添加'static''

限定符来实现。


顺便说一下,非抱怨的编译器没有错误。

编译器*允许*允许
$ b $中的非常量初始值设定项b除了标准要求的恒定初始化器之外。


-
Er ********* @ sun.com




" Eric Sosman" <尔********* @ sun.com>在留言中写道

新闻:40 *************** @ sun.com ...

"Eric Sosman" <Er*********@sun.com> wrote in message
news:40***************@sun.com...
抱怨编译器是正确的:a和b不是常量表达式。
尽管有拼写,但const不是常量。
The complaining compiler is correct: `a'' and `b'' are not
constant expressions.
Despite the spelling, `const'' is not
"constant."




是的。


const char * varname;





char const * varname;


前者表示varname [...]中的值是常量。例如


varname [0] =''a'';


会产生警告。


在后者中,实际指针varname是不变的


varname =& somebuf;


会产生警告,而


varname [0] =''a'';


不会。


const char const * varname;


将使两者保持不变。


Tom



Yes it is. There is just a diff between

const char *varname;

and

char const *varname;

The former means the values in varname[...] are constant. E.g.

varname[0] = ''a'';

will produce a warning.

In the latter the actual pointer "varname" is constant so

varname = &somebuf;

will produce a warning while

varname[0] = ''a'';

will not.

const char const *varname;

will make both constant.

Tom


这篇关于const数组声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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