Uses the use of imitations.
If you have a pointer p that points to an integer value, you access that integer value by dereferencing p using the unary operator * , as in
x = *p;
The type of the expression *p is int , so the declaration of p is
int *p;
The variable type p is "pointer to int " or int * . However, the declaration is written so that the expression type *p is int .
So, anytime you want to access the integer value pointed to by p , you must use *p . If all you need is the address value contained in p , you just use p . For example, dereferencing a NULL pointer results in undefined behavior, so at least you want to check that the value of the pointer is not NULL before trying to dereference it:
if (p != NULL) x = *p;
In the condition, we are dealing with the value contained in p , so we are not looking for it. In the statement, we are dealing with the value of p , so we are looking for it there.
Please note that the ad
int* p, q;
coincides with
int *p; int q;
in that only p declared as a pointer; q declared as a regular int .
source share