开发者

Declaration of strings in C [duplicate]

This question already has answers here: Closed 12 years ago. 开发者_JAVA百科

Possible Duplicates:

Is it possible to modify a string of char in C?

What is the difference between char s[] and char *s in C?

There is something I don't understand about strings and pointers in C.

Suppose I have this declaration:

char str[] = "abc";

Then, if I attempt to modify it this way:

str[0] = 'b';

It will work.

But if I declare the string as a pointer to a char

char* str = "abc"

The attempt above will cause an access violation.

What I'm trying to understand is what exactly is the difference.

Thanks in advance


In The later example the compiler puts the string in the read only data section, so u can't modify it. But in earlier example you are declaring array of character which resides in stack hence you can modify content of this array.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜