StringsByExample

From C

Jump to: navigation, search
 char *ptr;  char ary[4];

The first object stores a pointer value, to a single char. The second object stores 4 char values.

 char *ptr = "hello";

There are two objects here. The first object is the one named ptr. It stores a single character pointer value. The string literal "hello" is a second object of type char[]. It has no name. The compiler initializes the pointer value in blah to the address of the first character of the unnamed string literal object.

 char *a = "foo";
 char *b = "foo";

It is not defined whether a == b or not. The compiler may choose to store identical string literals as the same object or not.

 char *a = "Hello";  a[3] = 'p';  /* Bad assignment. */

String literal objects are not modifiable.

 char buf[] = "string";

There is one object here.

 char buf[] = {'s','t','r','i','n','g',0};

This declaration is equivalent to the previous one. In both cases, buf is defined as an array of 7 char values initialized to the 7 char values shown.

Personal tools