Search code examples
cpointersvalue-ofindirection

Declaring C Pointers - Why do we use * and not &


My question is a simple one (I hope) about c syntax regarding pointer declaration. I am fully aware of how to declare a pointer, how its used and what the effects are, like as follows.

int *val_ptr;
int val =99;

val_ptr = &val;

However, what confuses me is why when we declare a pointer in C do we use the * Indirection (value of) operator? and not the & address of operator. If we are declaring a pointer would it not make sense to do so with &, because we are declaring an address right? Example:

int & val_ptr;
int val =99;

val_ptr = &val;

I know it's incorrect but to my mind that would seem more intuitive. What is it I'm missing in my conception of the * operator. I have not yet found a text book that gives me an explanation of why, they just show how. I know how, I would like to know why.

Thanks for reading.


Solution

  • You can't use & operator to declare pointer in C. Standard doesn't allow this. When you declare pointer in C like:

    int *val_ptr;  
    

    then * in the declaration is not the dereference (indirection) operator. But when this * comes in a statement then it performs indirection.