sizeof和指针 [英] sizeof and pointer
问题描述
这是我的程序
....
char * p =" abcde";
char a [] =" ; abcde" ;;
.....
printf(p的大小是:%d \ n,sizeof(p) );
printf(a的大小是:%d \ n,sizeof(a));
.....
---------------------------------------------- -----------
,输出为:
p的大小为:1
a的大小是:6
------------------------
任何人都可以告诉我为什么?
this is my programm
....
char *p="abcde";
char a[]="abcde";
.....
printf("the size of p is:%d\n",sizeof(p));
printf("the size of a is:%d\n",sizeof(a));
.....
---------------------------------------------------------
and the output is:
the size of p is:1
the size of a is:6
------------------------
anyone can tell me why?
推荐答案
kevin写道:
kevin wrote:
这是我的程序
....
char * p =" abcde" ;;
char a [] =" abcde"; < br $>
.....
printf(p的大小为:%d \ n,sizeof(p));
printf(a的大小是:%d \ n,sizeof(a));
.....
- --------------------------------------- -----------------
,输出为:
p的大小为:1
a的大小是:6
------------------------
任何人都可以告诉我原因?
this is my programm
....
char *p="abcde";
char a[]="abcde";
.....
printf("the size of p is:%d\n",sizeof(p));
printf("the size of a is:%d\n",sizeof(a));
.....
---------------------------------------------------------
and the output is:
the size of p is:1
the size of a is:6
------------------------
anyone can tell me why?
你确定吗?周围没有很多系统(至少没有托管
!)和8位字符*。
p是一个字符*和一个是任何6个字符的数组,sizeof(char)是1,所以
sizeof(char [6])是6.
-
Ian Collins。
Are you sure? There aren''t many systems around (at least not hosted
ones!) with an 8 bit char*.
p is a char* and a is any array of 6 char, sizeof(char) is 1, so
sizeof(char[6]) is 6.
--
Ian Collins.
kevin写道:
kevin wrote:
这是我的程序
...
char * p =" abcde";
char a [] =" abcde";
。 ...
printf(p的大小是:%d \ n,sizeof(p));
printf(" a的大小是:%d \ n",sizeof(a));
....
----------- ----------------------------------------------
,输出为:
p的大小为:1
大小为:6
------------------------
任何人都可以告诉我为什么?
this is my programm
...
char *p="abcde";
char a[]="abcde";
....
printf("the size of p is:%d\n",sizeof(p));
printf("the size of a is:%d\n",sizeof(a));
....
---------------------------------------------------------
and the output is:
the size of p is:1
the size of a is:6
------------------------
anyone can tell me why?
我不认为你发布的代码是你运行的代码,除非你的
平台是sizeof(char) == sizeof(char *),这似乎不太可能
但可能。
字面答案 - 假设您的(不完整)代码真的是
您运行的是 - 指向char的指针的大小为1,
数组的大小为a(6)(6个字符,abcde加上最后的结果)。
-
Hewlett-Packard Limited注册号:
注册办事处:该隐Road,Bracknell,Berks RG12 1HN 690597英格兰
I don''t think the code you posted is the code you ran, unless your
platform is one where sizeof(char)==sizeof(char*), which seems unlikely
but possible.
The literal answer -- assuming that your (incomplete) code is really
what you ran -- is "the size of a pointer-to-char is 1, the size of the
array `a` is 6 (six chars, abcde plus the nul at the end)".
--
Hewlett-Packard Limited registered no:
registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England
凯文说:
kevin said:
这是我的programm
...
char * p =" abcde";
char a [] =" abcde" ;;
....
printf(p的大小是:%d \ n,sizeof(p));
printf(" si ze的一个是:%d \ nn,sizeof(a));
....
------------ ---------------------------------------------
,输出为:
p的大小为:1
a的大小为:6
------------------------
任何人都可以告诉我为什么?
this is my programm
...
char *p="abcde";
char a[]="abcde";
....
printf("the size of p is:%d\n",sizeof(p));
printf("the size of a is:%d\n",sizeof(a));
....
---------------------------------------------------------
and the output is:
the size of p is:1
the size of a is:6
------------------------
anyone can tell me why?
首先,让我们写一个正确的程序:
#include< stdio.h / * a prototype for printf is * REQUIRED * * /
int main(void)/ *可执行代码需要一个能够生活的函数* /
{
char * p =" abcde";
char a [] =" abcde";
printf(" size) p是%d \ n,(int)sizeof p);
printf(a的大小是%d \ n,(int)sizeof a);
返回0;
}
如果你得到1代表p,你有一个相当不寻常的系统,
字节是如此宽 - 可能是16位或更多 - 你可以将
指针放入一个。更有可能的是你输入了你的问题
而不是从真实程序中复制它。
如果你的问题只是为什么不是 t sizeof p和sizeof一样吗?",
这很容易。数组不是指针。指针不是数组。数组
确实非常大,但指针一般都很小
(虽然很少小到1,但我必须承认)。数组是城市。
指针是路标。当然,可以想象一个小到一个小的路标,但是一个人通常都不会打扰。
-
Richard Heathfield
Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk
电子邮件:rjh在上述域名中, - www。
First, let''s write a correct program:
#include <stdio.h/* a prototype for printf is *REQUIRED* */
int main(void) /* executable code needs a function in which to live */
{
char *p = "abcde";
char a[] = "abcde";
printf("the size of p is %d\n", (int)sizeof p);
printf("the size of a is %d\n", (int)sizeof a);
return 0;
}
If you are getting 1 for p, you have a rather unusual system, in which
bytes are so wide - probably 16 bits or more - that you can fit a
pointer into one. What is more likely is that you typed your question
instead of copying it from your real program.
If your question is simply "why aren''t sizeof p and sizeof a the same?",
that''s easy. Arrays are not pointers. Pointers are not arrays. Arrays
can be very large indeed, but pointers are generally quite small
(although rarely as small as 1, I must admit). Arrays are cities.
Pointers are signposts. It is possible to imagine a city as small as a
signpost, of course, but one doesn''t normally bother.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
这篇关于sizeof和指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!