Search code examples
cdereference

Confusion about the nature of * operator


I am confused between the following two declarations:

int *p=&a; //first

and

int *p; //second
p=&a;

Asterisk * is not acting as dereference operator. Most of the places I have seen that * acts as dereference operator. My question is that, apart from first case, is there any case where * operator does not act as dereference operator?


Solution

  • * acts as a dereference operator only in an expression.

    The (admittedly odd) syntax of C declarations is based on the idea that "declaration follows use". So these declarations:

    int i;
    int *p;
    

    can be read as "i is an int" and *p is an int. Since *p is an int, then p must be a pointer-to-int.

    (Of course * is also the multiplication operator if it appears with two operands.)