Do these statements about pointers have the same effect?

No.

char  str1[] = "Hello world!"; //char-array on the stack; string can be changed
char* str2   = "Hello world!"; //char-array in the data-segment; it's READ-ONLY

The first example creates an array of size 13*sizeof(char) on the stack and copies the string "Hello world!" into it.
The second example creates a char* on the stack and points it to a location in the data-segment of the executable, which contains the string "Hello world!". This second string is READ-ONLY.

str1[1] = 'u'; //Valid
str2[1] = 'u'; //Invalid - MAY crash program!

Leave a Comment