char *与const char * [英] char * vs. const char *
问题描述
嗨伙计
声称符合标准的C编译器是合法的
(c89)对以下内容发出错误:
char * foo(const char * s)
{
const char * s;
for(s = src; * s&&!(((unsigned char)s [0])& 0x80); s ++)
if(* s = = c)
返回s;
....
}
编译时遇到2个错误:
错误(212):返回值类型与函数类型不匹配
>
error(611):类型为const char *的值无法分配给实体
类型为char *
我会理解警告,但是出错了吗?
再见,Jojo
Hi folks
Is it legal for a C compiler that claims to be conforming to the standard
(c89) to issue an error on the following:
char *foo(const char *s)
{
const char *s;
for (s = src; *s && !(((unsigned char)s[0]) & 0x80); s++)
if (*s == c)
return s;
....
}
I get 2 errors when compiling this:
error(212): return value type does not match the function type
error(611): a value of type "const char *" cannot be assigned to an entity
of type "char *"
I''d understand a warning, but an error?
Bye, Jojo
推荐答案
Joachim Schmitz写道:
Joachim Schmitz wrote:
对C来说是否合法声称符合标准的编译器(c89)在以下内容上发出错误:
是的。事实上,一个符合标准的编译器/有义务/诊断这个
代码。
char * foo(const char * s)
const char * s;
[...]
我会理解一个警告,但是出错了吗?
Is it legal for a C compiler that claims to be conforming to the standard
(c89) to issue an error on the following:
Yes. In fact, a conforming compiler is /obliged/ to diagnose this
code.
char *foo(const char *s)
{
const char *s; [...]
I''d understand a warning, but an error?
没有C89中警告和错误之间的区别:
标准要求诊断和诊断。是针对某些违规行为发出的。在发出这样的诊断后,编译器没有继续翻译的义务。
Jeremy。
There''s no distinction between warnings and errors in C89: the
standard requires that "diagnostics" are issued for certain
violations. After such a diagnostic has been issued the compiler is
under no obligation to continue translation.
Jeremy.
>
嗨Jeremy
" Jeremy Yallop" < JE **** @ jdyallop.freeserve.co.uk>在消息中写道
news:sl ******************* @ maka.cl.cam.ac.uk ...
Hi Jeremy
"Jeremy Yallop" <je****@jdyallop.freeserve.co.uk> wrote in message
news:sl*******************@maka.cl.cam.ac.uk...
Joachim Schmitz写道:
Joachim Schmitz wrote:
声称符合
标准(c89)的C编译器对以下内容发出错误是否合法:
是。实际上,符合标准的编译器/有义务/诊断这个代码。
Is it legal for a C compiler that claims to be conforming to the standard (c89) to issue an error on the following:
Yes. In fact, a conforming compiler is /obliged/ to diagnose this
code.
char * foo(const char * s)
const char * s;
char *foo(const char *s)
{
const char *s;
[...]
[...]
我会理解一个警告,但是出错?
I''d understand a warning, but an error?
<在C89中,警告和错误之间没有区别:
标准要求诊断和诊断。是为了某些违法行为而发出的。在发出这样的诊断之后,编译器没有义务继续翻译。
There''s no distinction between warnings and errors in C89: the
standard requires that "diagnostics" are issued for certain
violations. After such a diagnostic has been issued the compiler is
under no obligation to continue translation.
谢谢,
好吧,不幸的是Samba代码充满了这些东西,我必须在所有地方插入
(char *)来编译...
如果允许编译器对此嗤之以鼻,这将是Samba的一个错误,我会
报告它。否则我会把它报告为对我们的c89的错误...
Jeremy。
Thanks,
Well, unfortunaly the Samba code is full of such things and I have to insert
(char *) all over the place to get it compiled...
If a compiler is allowd to choke on this, it''d be a bug in Samba and I''ll
report it as such. Otherwise I''d report it as a bug against our c89...
Jeremy.
再见,Jojo
Bye, Jojo
Joachim Schmitz< no *********** @ hp.com>写道:
Joachim Schmitz <no***********@hp.com> wrote:
嗨伙计
对于声称符合标准的C编译器(c89)是否合法,以便对以下内容发出错误:
char * foo(const char * s)
{
const char * s;
for(s = src; * s&&!(((unsigned char)s [0] )& 0x80); s ++)
if(* s == c)
return s;
...
}
编译时遇到2个错误:
error(212):返回值类型与函数类型不匹配
error(611) :类型为const char *的值不能分配给类型为char *的实体
我会理解一个警告,但是出错了吗?
Hi folks Is it legal for a C compiler that claims to be conforming to the standard
(c89) to issue an error on the following: char *foo(const char *s)
{
const char *s;
for (s = src; *s && !(((unsigned char)s[0]) & 0x80); s++) if (*s == c) return s; ... } I get 2 errors when compiling this: error(212): return value type does not match the function type error(611): a value of type "const char *" cannot be assigned to an entity
of type "char *" I''d understand a warning, but an error?
在这种情况下,编译器有义务发出诊断信息。是否
这是一个警告或错误是没有密切关系的标准
。
-
Alex Monjushko(mo*******@hotmail.com)
The compiler is obligated to emit a diagnostic in this case. Whether
it is a warning or an error is not germane as far as the standard is
concerned.
--
Alex Monjushko (mo*******@hotmail.com)
这篇关于char *与const char *的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!