查看: 3420| 回复: 15
跳转到指定楼层
上一主题 下一主题
收起左侧

[学C/C++] *s 与 s[] 的区别

全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
有两个c文件,代码如下:
(a.c)
char s[] = "hello world";

(b.c)
extern char *s;
int main() {
        puts(s);

}

为什么运行会出错??

上一篇:求问面试算法时能让用python的内置函数或者模块吗?
下一篇:設計MutableString
全局:
[转自stackoverflow]
这个人的回答很明确 希望可以帮助到lz


First off, in function arguments, they are exactly equivalent:

void foo(char *x);
void foo(char x[]); // exactly the same in all respects (note! this only applies if the brackets are empty)
In other contexts, char * allocates a pointer, while char [] allocates an array. Where does the string go in the former case, you ask? The compiler secretly allocates a static anonymous array to hold the string literal. So:

char *x = "Foo";
// is approximately equivalent to:
static const char __secret_anonymous_array[] = "Foo";
char *x = (char *) __secret_anonymous_array;
Note that you must not ever attempt to modify the contents of this anonymous array via this pointer; the effects are undefined (often meaning a crash):

x[1] = 'O'; // BAD. DON'T DO THIS.
Using the array syntax directly allocates it into new memory. Thus modification is safe:

char x[] = "Foo";
x[1] = 'O'; // No problem.
However the array only lives as long as its contaning scope, so if you do this in a function, don't return or leak a pointer to this array - make a copy instead with strdup() or similar. If the array is allocated in global scope, of course, no problem.
回复

使用道具 举报

推荐
cky9301 2017-3-29 07:48:33 | 只看该作者
全局:
这个应该是undefined behavior.

首先,char *和array of char不是同一种类型, 有些情况下array of char能够隐式转换为char *, 但是根据C/C++标准,LZ给的代码不会转换。申明和定义类型不同名字相同的变量应该是undefined behavior, compiler会任意进行实现。我在gcc上试了一下,运行时字符串的内容("hello world", asicii: 68 65 6c 6c 6f 20 77 6f 72 6c 64)直接被当成了地址0x6f77206f6c6c6568,随意就segfault了
回复

使用道具 举报

推荐
sskiso 2017-3-30 03:04:19 | 只看该作者
全局:
hunterli18 发表于 2017-3-30 02:12
谢谢各位!更谢谢cky9301 说的很详细!!!

ta没讲清原理,你去找一本 《C专家编程》 把第4和9章看一下,学C/C++你至少应该把这本书过几遍,新手必备,省了你走一年甚至多年弯路的时间。我晚上给你找一下那个pdf发给你。
回复

使用道具 举报

全局:
因为这两个s不是一个type吧。 一个是char [] ,一个是char*。
char [] memory 是在stack上的, char* 用heap memory, 正常用之前应该先malloc()一定的memory space给这个variable.
回复

使用道具 举报

🔗
 楼主| hunterli18 2017-3-29 04:33:44 | 只看该作者
全局:
动物园里的辛巴 发表于 2017-3-29 04:25
因为这两个s不是一个type吧。 一个是char [] ,一个是char*。
char [] memory 是在stack上的, char* 用he ...

所以运行的时候系统在heap上找不到s pointer就报错?
回复

使用道具 举报

全局:
本帖最后由 动物园里的辛巴 于 2017-3-29 05:30 编辑
hunterli18 发表于 2017-3-29 04:33
所以运行的时候系统在heap上找不到s pointer就报错?

我觉得你给这个code报错的原因是type mismatch吧,你这个extern s是char *,但你之前定义了s 是char[],这两个不是一个type

评分

参与人数 1大米 +10 收起 理由
CorinaW + 10

查看全部评分

回复

使用道具 举报

🔗
 楼主| hunterli18 2017-3-29 07:05:12 | 只看该作者
全局:
动物园里的辛巴 发表于 2017-3-29 05:29
我觉得你给这个code报错的原因是type mismatch吧,你这个extern s是char *,但你之前定义了s 是char[], ...

不是,compile link都可以通过,就是run时会crash。
回复

使用道具 举报

🔗
 楼主| hunterli18 2017-3-29 07:06:24 | 只看该作者
全局:
shiloh00 发表于 2017-3-29 05:51
[转自stackoverflow]
这个人的回答很明确 希望可以帮助到lz

谢谢,如果有具体到能回答我这个问题的答案更好了:)
回复

使用道具 举报

全局:
hunterli18 发表于 2017-3-29 07:06
谢谢,如果有具体到能回答我这个问题的答案更好了:)

额 虽然char[]表面上可以看成一个char* 但是他们是不一样的啊 一个是指针 一个是array 本身就是两种type啊
回复

使用道具 举报

🔗
wowmomsos 2017-3-29 08:13:27 | 只看该作者
全局:
记住,char*表常量, char[]表示字符串数组,可变。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表