difference between char *s and char s[]

In C, what's the difference between

char *s="Hello"; and char s[]="hello";

 

The difference here is that

char*s ="Hello";

will place Hello in the read-only parts of the memory and making s a pointer to that, making any writing operation on this memory illegal. While doing:

char s[]="Hello";

puts the literal string in read-only memory and copies the string to newly allocated memory on the stack. Making

s[0]='J';

legal.

 

Reference:http://stackoverflow.com/questions/1704407/what-is-the-difference-between-char-s-and-char-s-in-c

posted @ 2013-06-03 15:42  菜鸟的世界  阅读(217)  评论(0编辑  收藏  举报