char *a;
The above statements define a
to be a pointer to a character, i.e., it can store the address of a character.
char **c;
The above statement defines c
to be a pointer to a pointer to a character, i.e., it can store the address of a pointer to a character type, i.e., char *
variable.
a = &*c;
// equivalent to
a = &(*c)
// equivalent to
a = c;
c
is of type char **
, where as a
is of type char *
- a different type. These types are incompatible types which explains the warning message -
warning: assignment from incompatible pointer type
The variable c
is unitialized which means it contains garbage value, i.e., its value is indeterminate.
*c = "abcd";
The above statement means that a pointer to the first character of the string literal "abcd"
is stored in the location pointed to by c
, but the value of c
is garbage. It is undefined behaviour to dereference an uninitialized pointer. Undefined behaviour means unpredictable behaviour. Even though it may seem to work, anything can happen from program crash to your hard disk getting formatted. As such, there is no point in reasoning about code which invokes undefined behaviour. The standard imposes no requirement on the implementation to handle such cases and you should always avoid them.
Read these -
- Common undefined behaviour
- Undefined behaviour and sequence points