A pointer is a variable whose value is the address of another variable or in simple words a variable which points to another variable.
Here, myvar is a pointer type and can point to a integer variable. The asterisk * you used to declare a pointer is the same asterisk that you use for multiplication.
Few sample declaration
int *ip; /* pointer to an integer */
double *dp; /* pointer to a double */
float *fp; /* pointer to a float */
char *ch /* pointer to a character */
int x = 5;
ptr = &x;
pptr = &ptr;
Here x is an integer and ptr is a pointer pointing to the x. pptr is another pointer which is pointing to the ptr. x is having a value as 5 and support the address of x is 100 then value of ptr would be 100. Now assume value of ptr is 500 then pptr would have value as 500.
Null Pointers in C
It is a good practice to assign a NULL value to a pointer variable in case you do not have exact address to be assigned. Pointer that is assigned NULL is called a null pointer.
int *ptr = NULL; // ptr is a null pointer here
printf("The value of ptr is : %x\n", ptr );
Use Case To check for a null pointer one can use if statement as follows:
if(ptr) /* succeeds if p is not null */
if(!ptr) /* succeeds if p is null */
Address Operator (&) and pointers
It is denoted by & and used as a prefix of variable and returns the address of variable. It can be used only with variables not with literals (&5 is a invalid statement. Check the following program for the use case -
int main ()
int 5 = 5; /* actual variable declaration */
int *ip; /* pointer variable declaration */
ip = &i; /* store address of i in pointer variable*/
printf("Address of variable i: %x\n", &i);
printf("Address stored in variable ip: %x\n", ip);
printf("Value of *ip variable: %d\n", *ip);