The Pointer is simply a special variable that can hold the address of another variable. In general, every variable holds some kind of data based on its declaration. The declaration of pointer variable also looks like the declaration of regular variable but the difference is pointer stores memory address of a variable but not data.
Advantages of pointers:
1. Another identification for the memory location along with the variable name assigned by developer.
2. pointers can be used with arrays, functions, structures and data structures
3. Searching and sorting data
4. Dynamic memory allocation
5. modify value inside caller function by passing address of local variable to pointer
6. function can return more than one value using pointer
Declaration of pointer variable
As we know the declaration of pointer variable is almost same as the declaration of regular variable. The general declaration of pointer variable is:
*Identifier;
where:
datatype – any basic or user defined data type
Identifier – any valid identifier following naming conventions
Example:
int *ptr;
You can see the asterisk ‘*’ before the identifier of the pointer variable. The asterisk ‘*’ is not part of the pointer name but indicates that it is a pointer. The data type you can see before the name of the pointer specifying the data type of the variable whose address can be stored by your pointer.
Example:
int x;
float y;
int *ptr;
ptr=&x; //valid
ptr=&y; //invalid
The Address Of and Indirection Operators
The Address Of operator & preceding a variable returns the address of the variable associated with it. Other unary pointer operator is the “*”, also called as value at address or indirection operator. It returns a value stored at that address.
/* Program to print the address associated with a variable and value stored at that address*/
main( )
{
int qty = 5;
printf (“Address of qty = %un”,&qty);
printf (“Value of qty = %d n”,qty);
printf(“Value of qty = %d”,*(&qty));
}
OUTPUT
Address of qty = 65524
Value of qty = 5
Value of qty = 5
Pointer Type Declaration and Assignment
Like other variable, pointer must be declared first before it is used. The significant item to remember here is the data type specified in the declaration of pointer determines the data type of variable whose memory address is stored in the pointer.
Syntax to declare a pointer variable:
datatype *var_name;
Where * informs the compiler to declare a pointer variable
/*program describing pointers*/
# include
main( )
{
int qty = 5;
int *ptr; /* declares ptr as a pointer variable that points to an integer variable*/
ptr = &qty; /* assigning qty’s address to ptr -> Pointer Assignment */
printf (“Address of qty = %u n”, &qty);
printf (“Address of qty = %u n”, ptr);
printf (“Address of ptr = %u n”, &ptr);
printf (“Value of ptr = %d n”, ptr);
printf (“Value of qty = %d n”, qty);
printf (“Value of qty = %d n”, *(&qty));
printf (“Value of qty = %d”, *ptr);
}
OUTPUT
Address of qty = 65524
Address of ptr = 65522
Value of ptr = 65524
Value of qty = 5
Value of qty = 5
Value of qty = 5