返回指向常量字符串的指针 [英] Returning a pointer to a constant string
问题描述
我试图返回一个指向常量字符串的指针,但如果没有使用强制转换,编译器
会发出以下警告:
警告:从不兼容的指针类型分配
这是代码:
const char msg [] = 测试消息;
const char * message(void){
返回消息;
}
int main(void){
const char * str;
str =(const char *)message;
str =(char *)message;
str = message; / * GCC警告! * /
str =(const char *)msg;
str =(char *)msg;
str = msg ;
返回0;
}
奇怪的是,如果没有使用强制转换,GCC只会发出警告,但是如果演员放弃了const限定符,它就不会抱怨b $ b抱怨。这种行为
好吗?我正在使用GCC 4.1.2。谢谢!
祝你好运,
Santi
Hi,
I tried to return a pointer to a constant string, but the compiler
gives the following warning if a cast is not used:
warning: assignment from incompatible pointer type
This is the code:
const char msg[] = "Test message";
const char *message(void) {
return msg;
}
int main(void){
const char * str;
str = (const char *)message;
str = (char *)message;
str = message; /* GCC warning! */
str = (const char *)msg;
str = (char *)msg;
str = msg;
return 0;
}
Oddly, GCC only gives the warning if no cast is used, but it doesn''t
complain if the cast discards the const qualifier. Is this behavior
OK? I''m using GCC 4.1.2. Thanks!
Best regards,
Santi
推荐答案
在文章< 11 ********************** @ g4g2000hsf.googlegroups中。 com>,
=?iso-8859-1?q?Santiago_Urue = F1a?=< su ***** @ gmail.comwrote:
In article <11**********************@g4g2000hsf.googlegroups. com>,
=?iso-8859-1?q?Santiago_Urue=F1a?= <su*****@gmail.comwrote:
>我试图返回一个指向常量字符串的指针,但如果没有使用强制转换,编译器会给出以下警告:
>I tried to return a pointer to a constant string, but the compiler
gives the following warning if a cast is not used:
警告:从不兼容的指针类型分配
warning: assignment from incompatible pointer type
>这是代码:
>This is the code:
> const char msg [] =" test message" ;;
>const char msg[] = "Test message";
> const char * message(void){
return msg;
}
>const char *message(void) {
return msg;
}
密切注意const限定符的位置。
const char msg []说msg [someindex]将是一个const char
const char * message(void)
表示该消息将返回指向char的指针,并且
指针是常量。 (我想。我不是 - 积极的 - 我没有使用const很多次机会。)
-
一切都是虚荣。 - Ecclesiastes
Pay close attention to the placement of the const qualifiers.
const char msg[] says that msg[someindex] will be a const char
const char *message(void)
says that message will return a pointer to a char and that the
pointer is constant. (I think. I''m not -positive-. I haven''t had
much occasion to use const.)
--
All is vanity. -- Ecclesiastes
2007年9月17日星期一15:38:15 -0700,Santiago Urue?a
< su *** **@gmail.com写道:
On Mon, 17 Sep 2007 15:38:15 -0700, Santiago Urue?a
<su*****@gmail.comwrote:
>
我试图返回指向常量字符串的指针,但编译器<如果不使用演员表,则发出以下警告:
警告:从不兼容的指针类型分配
这是代码:
const char msg [] ="测试消息" ;;
const char * message(void){
return msg;
}
int main(void){
const char * str;
str =(const char *)message;
str =(char *)message;
str = message; / * GCC警告! * /
>Hi,
I tried to return a pointer to a constant string, but the compiler
gives the following warning if a cast is not used:
warning: assignment from incompatible pointer type
This is the code:
const char msg[] = "Test message";
const char *message(void) {
return msg;
}
int main(void){
const char * str;
str = (const char *)message;
str = (char *)message;
str = message; /* GCC warning! */
message是指向返回const char *的函数的指针。
之前的两个演员正在掩盖一个无端的错误!
Jim
message is a pointer to a function returning const char *. The
previous two casts are covering up a gratuitous mistake!
Jim
str =( const char *)消息;
str = (const char *)message;
str =(char *)message;
str = message; / * GCC警告! * /
str = (char *)message;
str = message; /* GCC warning! */
消息是指向返回const char *的函数的指针。
之前的两个演员正在掩盖一个无端的错误!
message is a pointer to a function returning const char *. The
previous two casts are covering up a gratuitous mistake!
你是对的!傻傻的我。
无论如何,编译器没有发出任何警告,即使限定符是由演员丢弃的
:
str =(const char *)message();
str =(char *)message(); / *没有警告! * /
str = message();
这样可以吗?
再次感谢。
Santi
You are right! Silly me.
Anyway, the compiler doesn''t give any warning even if the qualifier is
discarded by the cast:
str = (const char *)message();
str = (char *)message(); /* No warning! */
str = message();
Is this OK?
Thanks again.
Santi
这篇关于返回指向常量字符串的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!