Char *问题 [英] Char * question
本文介绍了Char *问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好,
我的pgm是
main()
{
char * c;
printf(" \ n c的大小是%d,sizeof(c));
c ="我是初学者;
printf(" \ n字符串c的大小是%d,sizeof(c));
}
两者的o / p案件是2
你能回答我为什么吗?
解决方案
smartbeginner说:
< blockquote class =post_quotes>你好,
我的pgm是
main()
{*> char * c;
printf(" \ n size of c是%d,sizeof(c));
这是你的第一个问题,与原型有关。请阅读以此消息ID开头的
最近的帖子:
< 11 *************** *******@f14g2000cwb.googlegroups .com>
提出了类似的问题并发生了类似的错误。
另外请注意,sizeof产生size_t,而不是int,因此%d是不合适的。
c =我是初学者;
printf(" \ n字符串c的大小是%d) ,sizeof(c));
}
两种情况下的o / p都是2
你能回答我为什么吗?
>
为对象赋值不会改变其大小。
-
Richard Heathfield
" ; Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk
电子邮件:rjh在上面的域名(但显然放弃了www)
char * c;
是一个字符指针;
c =我是初学者;
c的分配怎么样
因为c是一个指针,它总是占用两个字节来保存
地址,因此o / p是2。当你初始化指针c
时,它取字符串的起始地址,因此即使对于
,o / p也是2。
希望我是对的。祝你玩得愉快
Hello,
My pgm is
main()
{
char *c;
printf("\n Size of c is %d",sizeof(c));
c="Im a beginner";
printf("\n Size of string c is %d",sizeof(c));
}
The o/p in both cases is 2
Can you answer me why?
解决方案
smartbeginner said:
Hello,
My pgm is
main()
{
char *c;
printf("\n Size of c is %d",sizeof(c));
Here is your first problem, which is to do with prototypes. Please read a
recent thread which started with this message ID:
<11**********************@f14g2000cwb.googlegroups .com>
which asked a similar question and had a similar error.
Also note that sizeof yields size_t, not int, so %d is inappropriate.
c="Im a beginner";
printf("\n Size of string c is %d",sizeof(c));
}
The o/p in both cases is 2
Can you answer me why?
Assigning a value to an object does not change its size.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
char* c;
is a character pointer;
c= "Im a beginner";
what about the allocation of c
since c is a pointer it always occupies two bytes to hold the
address,hence the o/p is 2. when you have initialised the pointer c
,it takes the starting address of string,hence the o/p is two even for
that.
hope i''m correct.have a nice time
这篇关于Char *问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文