How to understand the pointer star * in C?
Take it this way:
int *i
means the value to which i points is an integer.
char **p
means that p is a pointer which is itself a pointer to a char.
int i; //i is an int.int *i; //i is a pointer to an intint **i;//i is a pointer to a pointer to an int.
Is the * sign interpreted differently in declarations and expressions?
Yes. They're completely different. in a declaration * is used to declare pointers. In an expression unary * is used to dereference a pointer (or as the binary multiplication operator)
Some examples:
int i = 10; //i is an int, it has allocated storage to store an int.int *k; // k is an uninitialized pointer to an int. //It does not store an int, but a pointer to one.k = &i; // make k point to i. We take the address of i and store it in kint j = *k; //here we dereference the k pointer to get at the int value it points //to. As it points to i, *k will get the value 10 and store it in j
The rule of declaration in c is, you declare it the way you use it.
char *p
means you need *p
to get the char,
char **p
means you need **p
to get the char.