char* p 与char p[] 的区别?
char* p = "Hello Linux!";
与
char p[] = "Hello Linux!";
的区别是?
是不是char* p = "Hello Linux!";相当于const char p[] = "Hello Linux!"; ???
问题点数:20、回复次数:24Top
1 楼huiminlee(huiminlee)回复于 2006-11-01 18:13:29 得分 0
指向的位置不同,char *p中p指向常量数据段,且p是可变的。char p[]中p指向局部数据段Top
2 楼Kenmark(fenix)回复于 2006-11-01 18:18:21 得分 0
你这里的*P是指向一个静态区的字符指针,你的P[]是一个数组,大小由编译器给予,客可以读写,*P不能Top
3 楼pcboyxhy(-273.15℃)回复于 2006-11-01 18:24:44 得分 0
是不是char* p = "Hello Linux!";相当于const char p[] = "Hello Linux!"; ???
不是
char* p = "Hello Linux!";
C语言标准并没有规定是否可以通过p修改""Hello Linux!",
如果用p修改"Hello Linux!",那么行为是未定义的
在某些编译器下会正确
在某些编译器下会错误
并不像一些人想象的那样
"Hello Linux!"存储在静态存储区
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 这只是某些编译器的实现而已,标准里面可没这么规定
换一个编译器,可能完全不是这回事了Top
4 楼lw1a2(一刀 现在改六点下班了:()回复于 2006-11-01 18:27:09 得分 0
TO楼上,在哪里写的,我去看看Top
5 楼pcboyxhy(-273.15℃)回复于 2006-11-01 18:35:45 得分 0
C99标准 第130页
32 EXAMPLE 8 The declaration
char s[] = "abc", t[3] = "abc";
defines "plain" char array objects s and t whose elements are initialized with character string
literals.
This declaration is identical to
char s[] = { 'a', 'b', 'c', '\0' },
t[] = { 'a', 'b', 'c' };
The contents of the arrays are modifiable.
On the other hand, the declaration
char *p = "abc";
defines p with type "pointer to char" and initializes it to point to an object with type
"array of char" with length 4 whose elements are initialized with a character string literal.
If an attempt is made to use p to modify the contents of the array, the behavior is undefined.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~Top
6 楼lockhall(唐朝 为了猥琐而努力学习)回复于 2006-11-01 19:45:34 得分 0
studying!Top
7 楼searingice(獨行·雪夜)(现实像石头,精神是个蛋。)回复于 2006-11-02 10:14:09 得分 0
studying too!Top
8 楼searingice(獨行·雪夜)(现实像石头,精神是个蛋。)回复于 2006-11-02 10:14:28 得分 0
studing three!Top
9 楼searingice(獨行·雪夜)(现实像石头,精神是个蛋。)回复于 2006-11-02 10:14:48 得分 0
studing four!Top
10 楼rainmanfan(rainman)回复于 2006-11-02 10:38:54 得分 0
这个问题我也困惑了很久,ls解答得好。Top
11 楼halfstudio()回复于 2006-11-02 12:44:48 得分 0
这个讨论意义不是很大,因为如果仅仅只是显示字符串,可以直接用指针;如果需要对字符进行操作运算,那么采用数组方式比较好,当然还有依具体的情况定。个人所见。Top
12 楼dadi0189(冰雨)回复于 2006-11-02 13:15:33 得分 0
char* p = "Hello Linux!";
在此句中"Hello Linux!";是字符串常量,p指向其首地址,p是指针变量,
可以赋值,例如,char a; p = &a;
char p[] = "Hello Linux!";
此句是数组赋值函数,p代表数组首地址,是数组名而不是指针变量.
不可以赋值,例如,char a; p = &a; 不能通过编译
Top
13 楼ymx0330(~~的士小子~~)回复于 2006-11-02 13:30:27 得分 0
pcboyxhy(-273.15℃)是个很人啊 !@¥%^&×()……Top
14 楼bohlee(我心澎湃)回复于 2006-11-02 13:34:05 得分 0
一个是指针,一个是数组Top
15 楼dingmin(专顶师太的小PP)回复于 2006-11-02 14:02:44 得分 0
照字面上来看,char* p 与char p[] 的区别
一个是指针,一个是数组;
照本质区别上来看,char* p 与char p[] 的区别
一个是静态指针,一个是动态指针;
Top
16 楼weixxxp(想一想)回复于 2006-11-02 14:14:51 得分 0
char *p :p是指针变量,其值可以修改,但不可以通过p修改指向的字符串
char p[] :p是数组变量,其值不可修改,但可以通过p修改指向的字符串Top
17 楼FFSB()...()回复于 2006-11-02 14:18:06 得分 0
LS:
p[] 就不存在p这个指针,仅仅是一个常量如345678H!Top
18 楼FFSB()...()回复于 2006-11-02 14:18:37 得分 0
LSLSLSTop
19 楼wht6688()回复于 2006-11-02 15:02:28 得分 0
lslsls有道理
Top
20 楼julong88((无锋之刃))回复于 2006-11-02 15:06:22 得分 0
不错Top
21 楼kj021320()回复于 2006-11-02 15:26:54 得分 0
*p 可以 p++ p-- 那些的!Top
22 楼Lenus()回复于 2006-11-02 16:10:35 得分 0
推荐你看看《C语言专家编程》这本书详细的介绍了数组和指针的区别~~~Top
23 楼vanter(vanter)回复于 2006-11-03 16:57:40 得分 0
sizeof(p) 的结果不同
一个等于sizeof(char *)
还有一个等于 strlen(p) + 1Top
24 楼argenCHN(【夷不谋夏,胡不乱华】)回复于 2006-11-03 22:00:29 得分 0
char *p :p是指针变量,其值可以修改,但不可以通过p修改指向的字符串
不能修改吗?
char p[] :p是数组变量,其值不可修改,但可以通过p修改指向的字符串
Top




