字符串文字的类型 [英] Type of a string literal
问题描述
我听说字符串文字的类型为''char *''(即指向
a char对象的指针) )。但我很困惑为什么这样做:
char * GetString(void)
{
return"你好;
}
int main(无效)
{
printf(" %s \ n",GetString());
返回0;
}
它打印''你好''。但是如果GetString()返回的指针
指向一个本地对象,那么当
函数返回时,不会释放缓冲区,从而使指针指向没有?或者
是否意味着字符串文字的类型为''static char *''?或者更多
',''static const char *''?
谢谢,
塞巴斯蒂安
s0****@gmail.com 写道:
我听说字符串文字的类型为''char *''(即指向
a char对象的指针。但我很困惑为什么这样做:
字符串文字的字符数组为char,但它自动转换为
指向它在大多数情境下的第一个元素的指针,对于C中的所有数组类型都是常见的
char * GetString(void)
{
return" hello" ;;
}
int main (无效)
{
printf("%s \ n",GetString());
返回0 ;
}
它会打印''你好''。但是如果GetString()返回的指针
指向一个本地对象,那么当
函数返回时,不会释放缓冲区,从而使指针指向没有?或者
是否意味着字符串文字的类型为''static char *''?或者更多
,''static const char *''?
谢谢,
Sebastian
源代码字符串文字放在静态数组中。它们的类型是char或wchar_t类型的
并且没有用const限定,但是修改
它们无论如何都会调用未定义的行为。
7月26日上午9:22,s0s ... @ gmail.com写道:
我听说字符串文字的类型为''char *''(即指向
a char对象的指针)。但我很困惑为什么这样做:
char * GetString(void)
{
return"你好;
}
见n1256.pdf中的6.4.5 p 5。字符串文字是不可变的,并且
被替换在TP 7中通过静态存储持续时间数组。它们实际的
类型是char [N],其中N是sizeof返回的整数,或者是wchar_t
[N](对于宽字符串文字)。
评估时,这些变为char *和wchar_t *。
请注意,GetString对程序员有用;返回值
无法修改!除非你记录下来,否则你的功能是误导的。
7月26日,5:03 * am,vipps ... @ gmail。 com写道:
7月26日上午9:22,s0s ... @ gmail.com写道:
您好,
我听说字符串文字的类型为''char *''(即指向
a char对象)。但我很困惑为什么这样做:
char * GetString(void)
{
* * return" hello" ;;
}
见n1256.pdf中的6.4.5 p 5。字符串文字是不可变的,并且
被替换在TP 7中通过静态存储持续时间数组。它们实际的
类型是char [N],其中N是sizeof返回的整数,或者是wchar_t
[N](对于宽字符串文字)。
评估时,这些变为char *和wchar_t *。
请注意,GetString对程序员有用;返回值
无法修改!除非你记录下来,否则你的功能是误导的。
误导。
谢谢。至于最后一部分:用'const'来限定
函数定义是不错的做法?
Hi,
I''ve heard that a string literal has type ''char *'' (i.e., a pointer to
a char object). But I''m confused as to why this works:
char *GetString(void)
{
return "hello";
}
int main(void)
{
printf("%s\n", GetString());
return 0;
}
And it does print ''hello''. But if the pointer that GetString() returns
points to a local object, wouldn''t the buffer be deallocated when the
function returns, thus making the pointer point to nothing? Or does
that mean that a string literal has type ''static char *''? Or more
precisely, ''static const char *''?
Thanks,
Sebastian
s0****@gmail.com wrote:
Hi,
I''ve heard that a string literal has type ''char *'' (i.e., a pointer to
a char object). But I''m confused as to why this works:A string literal has type array of char, but it automatically is
converted to a pointer to it''s first element under most contexts, as is
common for all array types in C.
char *GetString(void)
{
return "hello";
}
int main(void)
{
printf("%s\n", GetString());
return 0;
}
And it does print ''hello''. But if the pointer that GetString() returns
points to a local object, wouldn''t the buffer be deallocated when the
function returns, thus making the pointer point to nothing? Or does
that mean that a string literal has type ''static char *''? Or more
precisely, ''static const char *''?
Thanks,
SebastianSource code string literals are placed in static arrays. Their type is
of type char or wchar_t and are not qualified with const, but modifying
them invokes undefined behaviour anyway.
On Jul 26, 9:22 am, s0s...@gmail.com wrote:Hi,
I''ve heard that a string literal has type ''char *'' (i.e., a pointer to
a char object). But I''m confused as to why this works:
char *GetString(void)
{
return "hello";
}See 6.4.5 p 5 from n1256.pdf. string literals are immutable and
"replaced" in TP 7 by static storage duration arrays. Their actual
type is char [N] where N is the integer returned by sizeof, or wchar_t
[N] (for wide string literals).
When evaluated, these become char * and wchar_t *.
Notice that GetString lies to the programmer; the returned value
cannot be modified! Unless you document that, your function is
misleading.
On Jul 26, 5:03*am, vipps...@gmail.com wrote:On Jul 26, 9:22 am, s0s...@gmail.com wrote:
Hi,
I''ve heard that a string literal has type ''char *'' (i.e., a pointer to
a char object). But I''m confused as to why this works:
char *GetString(void)
{
* * return "hello";
}
See 6.4.5 p 5 from n1256.pdf. string literals are immutable and
"replaced" in TP 7 by static storage duration arrays. Their actual
type is char [N] where N is the integer returned by sizeof, or wchar_t
[N] (for wide string literals).
When evaluated, these become char * and wchar_t *.
Notice that GetString lies to the programmer; the returned value
cannot be modified! Unless you document that, your function is
misleading.Thanks. As for the last part: would it be good practice to qualify the
function definition with ''const''?
这篇关于字符串文字的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!