Sunday, August 30, 2015

char array, string literal, const


char string[] = "String literal";
string[0] = '[';

everything compiles; then seg fault.

    char* string1  = "String literal"
    string1[0] = '[';
    printf("%c\n", string1[0]);

compiles; prints

  char *p = "ab" ; // string literal, stores in read-only
  char p[] = "ab"; //can be manipulated
  char *p = (char []){'a', 'b', '\0'};
A character string literal is a sequence of zero or more multibyte characters enclosed in double-quotes

compilers replace string literals wtih pointers.

No comments: