Declaring a pointer variable


General syntax of pointer declaration is,

data-type *pointer_name;

Data type of pointer must be same as the variable, which the pointer is pointing. void type pointer works with all data types, but isn’t used oftenly.

Initialization of Pointer variable

Pointer Initialization is the process of assigning address of a variable to pointer variable. Pointer variable contains address of variable of same data type. In C language address operator & is used to determine the address of a variable. The & (immediately preceding a variable name) returns the address of the variable associated with it.

int a = 10 ;
int *ptr ;        //pointer declaration
ptr = &a ;        //pointer initialization
int *ptr = &a ;      //initialization and declaration together

Pointer variable always points to same type of data.

float a;
int *ptr;
ptr = &a;    //ERROR, type mismatch

Dereferencing of Pointer

Once a pointer has been assigned the address of a variable. To access the value of variable, pointer is dereferenced, using the indirection operator *.

int a,*p;
a = 10;
p = &a;   

printf("%d",*p);    //this will print the value of a. 

printf("%d",*&a);  //this will also print the value of a.

printf("%u",&a);  //this will print the address of a.

printf("%u",p);  //this will also print the address of a.

printf("%u",&p);  //this will also print the address of p.