Strings in C are represented as arrays of characters.
char *p = "String";
You declare a pointer pointing to a line stored somewhere in your program (changing this line is undefined behavior) according to the C 2 ed programming language.
char p2[] = "String";
You declare a char array initialized with the string "String", leaving the task to the compiler to calculate the size of the array.
char p3[5] = "String";
You declare an array of size 5 and initialize it to "String". This is a mistake because "String" does not fit into 5 elements.
char p3[7] = "String"; is the correct declaration ('\ 0' is the trailing character in lines c).
http://c-faq.com/~scs/cclass/notes/sx8.html
obo Jan 04 2018-12-12T00: 00Z
source share