求教char *s and char[] s的区别

lantu007 2009-09-26 12:56:39

char s1[] = "hello";
char *s2 = "hello";


很显然,s1是一个字符数组(以'\0'结尾),在C语言中,可以把数组名s1当作一个指针(数组首地址)来看待(但不完全是,《C expert programming》上是这么说的),的确因为 *s1 = 'a',所以s1貌似是个指针,但为什么sizeof(s1)会等于6而不是2(或者4,因C编译器而异)?

相对应地,sizeof(s2)=2,显然,s2的确是一个指针。那么s1究竟是指针吗?如果不完全是,究竟是怎样的呢?
...全文
151 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
2009-09-26
  • 打赏
  • 举报
回复
数组的确不是指针!
只是数组可以隐式类型转换为指向第一个元素的指针。
teng0210 2009-09-26
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 hpsmouse 的回复:]
6.3.2.1 Lvalues, arrays, and function designators
1 An lvalue is an expression with an object type or an incomplete type other than void; if an lvalue does not designate an object when it is evaluated, the behavior is undefined. When an object is said to have a particular type, the type is specified by the lvalue used to designate the object. A modifiable lvalue is an lvalue that does not have array type, does not have an incomplete type, does not have a const-qualified type, and if it is a structure or union, does not have any member (including, recursively, any member or element of all contained aggregates or unions) with a const-qualified type.

数组是 lvalue,但不是 modifiable lvalue。
[/Quote]
I get it.
2009-09-26
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 teng0210 的回复:]
数组名不是左值
数组名是常量
char a[]="123";
a+=1;//错误
char *p=a;
p+=1;//正确
[/Quote]
数组名是左值,标准里说得很清楚:

6.3.2.1 Lvalues, arrays, and function designators
1 An lvalue is an expression with an object type or an incomplete type other than void; if an lvalue does not designate an object when it is evaluated, the behavior is undefined. When an object is said to have a particular type, the type is specified by the lvalue used to designate the object. A modifiable lvalue is an lvalue that does not have array type, does not have an incomplete type, does not have a const-qualified type, and if it is a structure or union, does not have any member (including, recursively, any member or element of all contained aggregates or unions) with a const-qualified type.

数组是 lvalue,但不是 modifiable lvalue。
非兔子_Logic0 2009-09-26
  • 打赏
  • 举报
回复
glacier3d 2009-09-26
  • 打赏
  • 举报
回复
数组类型和指针类型是有本质区别的,编译器在处理数组类型时,是有尺寸信息的,指针没有,*(p+数)等都只是一种寻址方式
lihan6415151528 2009-09-26
  • 打赏
  • 举报
回复
上帝是神仙(假设是个男神仙)
亚当是男人,夏娃是女人

上帝要用一个人去打扫他的房间,则亚当和夏娃都可以使用
当上帝想要发泄欲望的时候,显然只有夏娃可以使用
xiaoyuer5222 2009-09-26
  • 打赏
  • 举报
回复
补充一点:
char s1[] = "hello";
char *s2 = "hello";

你可以这样 *(s1 + 2) = 'x';
但不可以这样 *(s2 + 2) = 'x';

说明:标准规定,用初始化指针所用的字符串常量被定义为只读,若试图修改其值,将会出现未定义的错误
Z782282738 2009-09-26
  • 打赏
  • 举报
回复
数组是数组
teng0210 2009-09-26
  • 打赏
  • 举报
回复
数组名不是左值
数组名是常量
char a[]="123";
a+=1;//错误
char *p=a;
p+=1;//正确
xiaoyuer5222 2009-09-26
  • 打赏
  • 举报
回复
char s1[] = "hello";
s1是一个数组,它是有存储空间的,所以sizeof(s1)当然是6。
然而数组名又是数组的首地址,所有有*s1 = 'h'
char *s2 = "hello";
s2 是一个指针,s2 = "hello";只是用常量字符串"hello"的地址来初始化这个指针,不是将字符串的值赋给它,sizeof(s2)=4,指针的存储空间大小
shiweifu 2009-09-26
  • 打赏
  • 举报
回复
只是在函数中,数组和指针的概念可以互换的
声明的时候数组是数组,指针是指针
编译器对于两者的解释方式是不同的
指针保存的是内存地址
而数组就是在数组名所表示的内存地址中保存数据

LZ看下《C专家编程》吧,其中有讲到
wen_long2008 2009-09-26
  • 打赏
  • 举报
回复
我想是不是这样呢:
1、char str[]="****";
是在定义一个字符数组,数组元素个数没有显式指定,
字符串以\0结尾,所以长度会比实际的长度多1
既然如此,那么sizeof就是字符的个数+1啦。
2、char*str=“*********”,确实是在定义一个字符指针,
长度是4字节(各种类型的指针位数都是一样的,只是进行指针运算时候不同,
要视指针类型来定)
3、数组的首地址是数组名,也是第一个元素的地址,是不是编译器在处理的时候
按后者了。
理解不对,请指正,谢谢!

2009-09-26
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 aizibion 的回复:]
就好比:
上帝是神仙(假设是个男神仙)
亚当是男人,夏娃是女人

上帝要用一个人去打扫他的房间,则亚当和夏娃都可以使用
当上帝想要发泄欲望的时候,显然只有夏娃可以使用
[/Quote]
这个比喻…………
aizibion 2009-09-26
  • 打赏
  • 举报
回复
这个问题真的让人很疑惑,不过想通了其实也就那麽回事
就类型而言,指针和数组本就是两个东西,这个没什么好迷惑人的
就使用而言,数组名一般被退化为指向第一个元素的指针,那麽真正的指针和退化了的数组名也没什么区别了
但是针对一些诸如sizeof的对类型的操作上,我们应该清晰地明白是就类型而言

就好比:
上帝是神仙(假设是个男神仙)
亚当是男人,夏娃是女人

上帝要用一个人去打扫他的房间,则亚当和夏娃都可以使用
当上帝想要发泄欲望的时候,显然只有夏娃可以使用



69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧