sizeof(< string literal>) [英] sizeof( <string literal> )

查看:82
本文介绍了sizeof(< string literal>)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当应用于字符串文字时,sizeof运算符是否应该返回字符串的大小

(包括nul),还是指针的大小?


例如,假设一个字符是1个字节而一个字符*是4个字节,那么下面是否会产生4个,5个

的其他东西? (并且,如果还有别的,决定结果的是什么?)


char x [] =" abcd";

printf("%d \\ \\ n",sizeof(x));


-Don

解决方案

Don Starr写道:

当应用于字符串文字时,sizeof运算符应该返回字符串的大小
(包括nul),还是指针的大小?


闻起来像家庭作业。

例如,假设一个字符是1个字节而一个char *是4个字节,那么下面应该得到4个,5个还有别的吗? (而且,如果还有什么,决定结果的是什么?)


是的。真的闻起来像家庭作业。

char x [] =" abcd";
printf("%d \ n",sizeof(x));




嗯,你觉得怎么样?

[提示:你这里没有采用字符串文字的'sizeof'',但是,

而不是一个'char''数组]


HTH,

--ag


-

Artie Gold - 德克萨斯州奥斯汀


Don Starr写道:


当应用于字符串文字时,sizeof运算符应该返回字符串
(包括nul)的大小,还是指针的大小?

例如,假设一个字符是1个字节而一个字符*是4个字节,那么下面是否会产生其他的东西? (并且,如果有其他因素,决定结果的是什么?)

char x [] =" abcd";
printf("%d \ n",sizeof(x) );




你会得到5.(实际上你可能会得到未定义的行为

因为`sizeof''运算符会产生结果类型为`size_t'',

和%d并不一定是正确的格式说明符。我已经使用过上面会使用的机器
打印0!)


为什么5?因为字符串文字会生成一个

个字符的数组。当您将`sizeof''运算符应用于数组

(任何数组)时,您将获得数组中的字节数。这个

是极少数情况下数组引用的情况之一

*不*变成指向第零个元素的指针; 'sizeof''

运算符作为一个整体应用于数组。


第6节 - 在comp.lang.c中常见问题

(常见问题)列表

http://www.eskimo.com/~scs/C-faq/top.html


-
呃******* **@sun.com


Don Starr< no **** @ nospam.net>写道:

当应用于字符串文字时,sizeof运算符
应该返回字符串的大小(包括nul),或者
的大小一个指针?


sizeof运算符产生其操作数类型的大小。如果

其操作数具有指针类型,则它产生

指针类型的大小。如果它的操作数有一个数组类型,那么它会产生数组中字节数的


字符串没有特殊情况。

例如,假设char是1字节


字符总是1个字节。

和一个char *是4个字节,如果以下产生4,5,
其他什么? (并且,如果还有什么,是什么决定了
结果?)

char x [] =" abcd";
printf("%d \ n", sizeof(x));




你需要在这里将sizeof的结果强制转换为int,因为sizeof

产生的类型为size_t ,这是一种无符号类型。


通过该校正,这将始终打印5,因为数组

有五个char类型的元素。

-

int main(void){char p [] =" ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz。\

\ n",* q =" ; kl BIcNBFr.NKEzjwCIxNJC" ;; int i = sizeof p / 2; char * strchr(); int putchar(\

); while(* q){i + = strchr(p,* q ++) )-p; if(i> =(int)sizeof p)i- = sizeof p-1; putchar(p [i] \

);} return 0;}


When applied to a string literal, is the sizeof operator supposed to return the size of the string
(including nul), or the size of a pointer?

For example, assuming a char is 1 byte and a char * is 4 bytes, should the following yield 4, 5, of
something else? (And, if something else, what determines the result?)

char x[] = "abcd";
printf( "%d\n", sizeof( x ) );

-Don

解决方案

Don Starr wrote:

When applied to a string literal, is the sizeof operator supposed to return the size of the string
(including nul), or the size of a pointer?
Smells like homework.

For example, assuming a char is 1 byte and a char * is 4 bytes, should the following yield 4, 5, of
something else? (And, if something else, what determines the result?)
Yup. Really smells like homework.

char x[] = "abcd";
printf( "%d\n", sizeof( x ) );



Well, what do you think?
[hint: you''re not taking the `sizeof'' of a string literal here, but,
rather, an array of `char'']

HTH,
--ag

--
Artie Gold -- Austin, Texas


Don Starr wrote:


When applied to a string literal, is the sizeof operator supposed to return the size of the string
(including nul), or the size of a pointer?

For example, assuming a char is 1 byte and a char * is 4 bytes, should the following yield 4, 5, of
something else? (And, if something else, what determines the result?)

char x[] = "abcd";
printf( "%d\n", sizeof( x ) );



You''ll get 5. (Actually you might get undefined behavior
because the `sizeof'' operator yields a result of type `size_t'',
and "%d" isn''t necessarily the right format specifier. I''ve
used machines on which the above would print 0!)

Why 5? Because a string literal generates an array of
characters. When you apply the `sizeof'' operator to an array
(any array), you get the number of bytes in the array. This
is one of the very few cases where an array reference does
*not* turn into a pointer to the zero''th element; the `sizeof''
operator applies to the array as a whole.

See also Questions 6.4 and 6.8 -- in fact, read all of
Section 6 -- in the comp.lang.c Frequently Asked Questions
(FAQ) list

http://www.eskimo.com/~scs/C-faq/top.html

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


Don Starr <no****@nospam.net> writes:

When applied to a string literal, is the sizeof operator
supposed to return the size of the string (including nul), or
the size of a pointer?
The sizeof operator yields the size of its operand''s type. If
its operand has a pointer type, then it yields the size of the
pointer type. If its operand has an array type, then it yields
the number of bytes in the array. There is no special case for a
string.
For example, assuming a char is 1 byte
A char is always 1 byte.
and a char * is 4 bytes, should the following yield 4, 5, of
something else? (And, if something else, what determines the
result?)

char x[] = "abcd";
printf( "%d\n", sizeof( x ) );



You need to cast the result of sizeof to int here, because sizeof
yields a result of type size_t, which is an unsigned type.

With that correction, this will always print 5, because the array
has five elements of type char.
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}


这篇关于sizeof(&lt; string literal&gt;)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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