C语言中字符串数组与字符指针数组有什么区别?怎么用?

Jether 2009-11-15 09:32:55
C语言中字符串数组与字符指针数组有什么区别?怎么用?
...全文
28477 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
亚细亚 2009-11-16
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 selooloo 的回复:]
引用 3 楼 huiguixian 的回复:
引用 1 楼 cattycat 的回复:
char str[]="hello world!"; //字符串数组
char* str1[]={"hello","world","test char array"}; //字符串指针数组

字符串指针数组的元素是字符串指针,可以指向字符串,这是比较简单的例子。


字符串数组比如
char str[2]={"hello" ,"world!"}; //字符串数组
char* str1[]={"hello","world","test char array"}; //字符串指针数组


char str[][10]={"hello" ,"world!"}; //字符串数组
char* str1[]={"hello","world","test char array"};//字符串指针数组

字符串数组分配了空间,比如str数组的大小为2*10字节;str1数组存放的都是指针,大小为4*3字节
将str赋值给str1可以这样for(i=0;i <2;i++)    str1[i]=str[i];不过只是指针传递,并不是值传递。
反过来str[i]=str1[i];就不行
[/Quote]
正确!
happy08god 2009-11-16
  • 打赏
  • 举报
回复
selooloo说得好!
selooloo 2009-11-15
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 huiguixian 的回复:]
引用 1 楼 cattycat 的回复:
char str[]="hello world!"; //字符串数组
char* str1[]={"hello","world","test char array"}; //字符串指针数组

字符串指针数组的元素是字符串指针,可以指向字符串,这是比较简单的例子。


字符串数组比如
char str[2]={"hello" ,"world!"}; //字符串数组
char* str1[]={"hello","world","test char array"}; //字符串指针数组

[/Quote]
char str[][10]={"hello" ,"world!"}; //字符串数组
char* str1[]={"hello","world","test char array"};//字符串指针数组

字符串数组分配了空间,比如str数组的大小为2*10字节;str1数组存放的都是指针,大小为4*3字节
将str赋值给str1可以这样for(i=0;i<2;i++) str1[i]=str[i];不过只是指针传递,并不是值传递。
反过来str[i]=str1[i];就不行
小小攻城师 2009-11-15
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 cattycat 的回复:]
char str[]="hello world!"; //字符串数组
char* str1[]={"hello","world","test char array"}; //字符串指针数组

字符串指针数组的元素是字符串指针,可以指向字符串,这是比较简单的例子。

[/Quote]
字符串数组比如
char str[2]={"hello" ,"world!"}; //字符串数组
char* str1[]={"hello","world","test char array"}; //字符串指针数组
aizibion 2009-11-15
  • 打赏
  • 举报
回复
一个装指针,一个装字符的区别
cattycat 2009-11-15
  • 打赏
  • 举报
回复
char str[]="hello world!"; //字符串数组
char* str1[]={"hello","world","test char array"}; //字符串指针数组

字符串指针数组的元素是字符串指针,可以指向字符串,这是比较简单的例子。

69,382

社区成员

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

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