char *, char[]

Tags:

char *str=”CONSTANT STRING” is constant?

Since, xxxx – I don’t know when it happend. -_-; -, char * is no longer char array variable.

If you declare a variable like char *str=”…”; contents of str becomes const. So, modification such as str[1]=’1′ is not valid. (It raises segmentation fault.)

Long time ago, char *str was implicitly converted to string variable (For the time being, please apologize incorrect designations I’m using. I’m not C language spec expert.), so str[1]=’1′ was possible.

Constrast

char *str=”…”

with

char str[]=”…”

The former is ‘string literal’, but the latter is ‘characters which is stored in array.’

Do you know the difference between * and []?

* is pointer.
[] is pointer constant.

i.e.,

char *str1=”…”
char *str2=”…”

str1=str2; // This is valid.

However,

char []str1=”…”
char *str2=”…”

str1=str2; // This is not valid because str1 is ‘constant’, it can not be modifed.

But,

str[1]=’1′; // This is ok. Contents of str1 is not constant.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *