常见问题 [英] const problem
问题描述
我已经将int声明为const但编译器仍然说它不是
const:
包含< stdio.h>
#include< stdlib.h>
int main()
{
const int MAXSIZE = 100;
char ac [MAXSIZE] =" abc" ;;
char * pc;
for(pc = ac; * pc !=''\'''; ++ pc)
{
printf("%c\ n",* pc);
}
返回EXIT_SUCCESS;
}
============输出==============
[arnuld @ raj C] $ gcc -ansi -pedantic -Wall -Wextra test.c
test .c:在函数main中:
test.c:8:警告:ISO C90禁止可变大小的数组`ac''
test.c:8 :错误:可变大小的对象可能无法初始化
[arnuld @ raj C] $
K& R2第2.4节说:
"对于一个数组,const限定符表示元素不会被改变为
。
但我甚至无法编译程序。
[arnuld @ raj C] $ gcc --version
gcc(GCC)3.4.3 20041212(Red Hat 3.4.3-9.EL4)
Copyright(C)2004 Free Software Foundation,Inc。
这是免费软件;查看复制条件的来源。有
无保修;甚至不是为了适销性或特定的适用性
目的。
-
http://lispmachine.wordpress.com/
请删除大写''V'的时候你通过电子邮件回复我。
I have declared an int as const but compiler still says that it is not a
const:
include <stdio.h>
#include <stdlib.h>
int main()
{
const int MAXSIZE = 100;
char ac[MAXSIZE] = "abc";
char *pc;
for( pc = ac; *pc != ''\0''; ++pc )
{
printf("%c\n", *pc);
}
return EXIT_SUCCESS;
}
============ OUTPUT ==============
[arnuld@raj C]$ gcc -ansi -pedantic -Wall -Wextra test.c
test.c: In function `main'':
test.c:8: warning: ISO C90 forbids variable-size array `ac''
test.c:8: error: variable-sized object may not be initialized
[arnuld@raj C]$
K&R2 section 2.4, says:
"For an array, the const qualifier says that elements will not be
altered."
but I can''t even compile the program.
[arnuld@raj C]$ gcc --version
gcc (GCC) 3.4.3 20041212 (Red Hat 3.4.3-9.EL4)
Copyright (C) 2004 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is
NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.
--
http://lispmachine.wordpress.com/
Please remove capital ''V''s when you reply to me via e-mail.
推荐答案
gcc -ansi -pedantic -Wall -Wextra test.c
test.c:在函数main中:
test.c:8:警告:ISO C90禁止可变大小的数组`ac''
test.c:8:错误:可能无法初始化可变大小的对象
[arnuld @ raj C]
gcc -ansi -pedantic -Wall -Wextra test.c
test.c: In function `main'':
test.c:8: warning: ISO C90 forbids variable-size array `ac''
test.c:8: error: variable-sized object may not be initialized
[arnuld@raj C]
K& R2第2.4节说:
"对于数组,const限定符表示元素不会被改变。\\ b
。
但是我甚至无法编译程序。
[arnuld @ raj C]
K&R2 section 2.4, says:
"For an array, the const qualifier says that elements will not be
altered."
but I can''t even compile the program.
[arnuld@raj C]
gcc --version
gcc(GCC)3.4.3 20041212 (Red Hat 3.4.3-9.EL4)
版权所有(C)2004自由软件基金会,公司
这是免费软件;查看复制条件的来源。有
无保修;甚至不是为了适销性或特定的适用性
目的。
-
http://lispmachine.wordpress.com/
请删除大写''V'的时候你通过电子邮件回复我。
gcc --version
gcc (GCC) 3.4.3 20041212 (Red Hat 3.4.3-9.EL4)
Copyright (C) 2004 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is
NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.
--
http://lispmachine.wordpress.com/
Please remove capital ''V''s when you reply to me via e-mail.
这篇关于常见问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!