struct中的char *是可重写的,但是在main()函数中却没有。为什么?

时间:2021-05-17 21:42:42

I wonder the difference of same char* between when it resides in struct and in main() function.

我想知道它在struct和main()函数中的相同char *之间的区别。

Here is the code:

这是代码:

struct student {
    char* name;
};

int main() {

    // char* in struct
    struct student bob;
    bob.name = "alice";
    bob.name = "bob";
    printf("name: %s\n", bob.name);

    // char* in main()
    char *name = "kim";
    *name = "lee";
    printf("name: %s\n", name);

    return 0;
}

Output:

name: bob
name: kim

In the case of using struct, the value of student bob.name was changed from "alice" to "bob". However, in the latter case, the value of char* name wasn't changed.

在使用struct的情况下,student bob.name的值从“alice”更改为“bob”。但是,在后一种情况下,char * name的值没有改变。

I think that the reason why "kim" didn't changed to "lee" is char *name was pointing literal "kim".

我认为“kim”没有改为“lee”的原因是char * name指向文字“kim”。

If I'm right, why bob.name was changed from "alice" to "bob"? It shoudn't have changed to "bob" because "alice" was also literal.

如果我是对的,为什么bob.name从“alice”改为“bob”?它并没有变成“鲍勃”,因为“爱丽丝”也是字面意思。

What's the difference?

有什么不同?

2 个解决方案

#1


3  

Your code invokes Undefined Behavior,

您的代码调用未定义的行为,

since you do:

既然你这样做:

struct student { char* name;};
struct student bob;
bob.name = "alice";

i.e. you are making the pointer name, point to a string literal.

即你正在制作指针名称,指向字符串文字。

Then you just:

然后你就是:

bob.name = "bob";

make the pointer point to another string literal, which is OK, since you just modify where the pointer points to, not the string literal that it points to (for example bob.name[3] = 'f';, which would lead in a Segmentation Fault, since it would attempt to modify a string literal - forbidden).

使指针指向另一个字符串文字,这是正常的,因为你只是修改指针指向的位置,而不是它指向的字符串文字(例如bob.name [3] ='f';,这将导致分段错误,因为它会尝试修改字符串文字 - 禁止)。

If you plan to make your pointer point to string literals, then I suggest you declare it that way:

如果你打算让你的指针指向字符串文字,那么我建议你这样说:

const char* name;

which allows you to change where the pointer points to, but not the contents of the string literal the pointers points to.

它允许您更改指针指向的位置,但不能更改指针指向的字符串文字的内容。


Now this:

char *name = "kim";

is a string literal, where its contents cannot be modified. And when you do:

是一个字符串文字,其内容无法修改。当你这样做时:

*name = "lee";

you just do something that it's not allowed, resulting an ill-formed program.

你只是做了一些不允许的事情,导致程序结构不合理。

#2


0  

Your example does not compile.

您的示例无法编译。

*name = "lee"; // Error 1 error C2440: '=' : cannot convert from 'const char [4]' to 'char'

* name =“lee”; //错误1错误C2440:'=':无法从'const char [4]'转换为'char'

The reason is that *name points to a char and "lee" is a char[4].

原因是* name指向char,“lee”是char [4]。

#1


3  

Your code invokes Undefined Behavior,

您的代码调用未定义的行为,

since you do:

既然你这样做:

struct student { char* name;};
struct student bob;
bob.name = "alice";

i.e. you are making the pointer name, point to a string literal.

即你正在制作指针名称,指向字符串文字。

Then you just:

然后你就是:

bob.name = "bob";

make the pointer point to another string literal, which is OK, since you just modify where the pointer points to, not the string literal that it points to (for example bob.name[3] = 'f';, which would lead in a Segmentation Fault, since it would attempt to modify a string literal - forbidden).

使指针指向另一个字符串文字,这是正常的,因为你只是修改指针指向的位置,而不是它指向的字符串文字(例如bob.name [3] ='f';,这将导致分段错误,因为它会尝试修改字符串文字 - 禁止)。

If you plan to make your pointer point to string literals, then I suggest you declare it that way:

如果你打算让你的指针指向字符串文字,那么我建议你这样说:

const char* name;

which allows you to change where the pointer points to, but not the contents of the string literal the pointers points to.

它允许您更改指针指向的位置,但不能更改指针指向的字符串文字的内容。


Now this:

char *name = "kim";

is a string literal, where its contents cannot be modified. And when you do:

是一个字符串文字,其内容无法修改。当你这样做时:

*name = "lee";

you just do something that it's not allowed, resulting an ill-formed program.

你只是做了一些不允许的事情,导致程序结构不合理。

#2


0  

Your example does not compile.

您的示例无法编译。

*name = "lee"; // Error 1 error C2440: '=' : cannot convert from 'const char [4]' to 'char'

* name =“lee”; //错误1错误C2440:'=':无法从'const char [4]'转换为'char'

The reason is that *name points to a char and "lee" is a char[4].

原因是* name指向char,“lee”是char [4]。