Pointer is C is to makes the code more efficient and compact. C is a very powerful language and the real power of C lies in pointers. Pointer is also a variable which will hold the address of other variable for which it is declared.
The General format is – Data_type *pv;
Example :- int *p;
where p is pointer variable which will hold the address of integer variable.
Some of the user of pointers are:
- Simulating call be reference.
- Returning more than one value form a function.
- Accessing dynamically allocated memory.
- Implementing data structure like linked lists, trees and graphs.
- Improving efficiency.
Memory : The memory in computer is made up of bytes arranged in a sequential manner. Each byte is identified by an index number, which is called the address of that byte. The address of these bytes start from zero and the address of last byte is one less than the size of memory.
Address Operator: C provides an address operator ‘&’ , which returns the address of a variable when placed before it. This operator can be read as “the address of” , so &age means address of variable age , similarly &salary means address of variable salary .
The following programs prints the address of variable using address operator.
Example : Program to print address of variable using address operator?
int age =30;
float salary = 1500.50;
printf(“Address of age= %p\n”,&age);
printf(“Address of salary= %p\n”,&salary);
Address of age = 0012FED4
Address of salary = 0012FEC8
Pointers Variable : A pointer is a variable that stores memory address. like all others variable it also has a name, has to be declared and occupies some space in memory. It is called as pointer because it points to a particular location in memory by storing the address of that location.
Declaration of Pointer Variable
The general syntax of declaration is :- data_type *pname;
Here pname is the name of pointer variable which should be a valid c identifier. The asterisk ‘*’ preceding this name informs the compiler that the variable is declared as a pointer. Here data_type is knows as the base type of pointer.
Pointers are variable so compiler will reserve space for them and they will also have some address. All pointers irrespective of their base type generally occupy same space in memory. Now the question arises that when all the pointers contains addresses only and each one occupy same space.
Assigning Address to Pointer Variable
Global and local static pointer are automatically initialized to NULL but when we declare an automatic pointer variable it contains garbage value that is it may be pointing anywhere in the memory, So we should always assign an address before using it in the program. The use of an unassigned pointer may give unpredictable results and even cause the program to crash. Pointers can be assigned the address of a variable using assignment statement.
Example : int *iptr, age=30;
Now iptr contains the address of variable age that it points to variable age.
All types of arithmetic operations are not possible with pointer. The only valid operation that can be performed are –
- Addition of an integer to a pointer and increment operation.
- Subtracting of an integer from a pointer and decrement operation.
- Subtracting of a pointer from another pointer of same type.
Pointer arithmetic is somewhat different from ordinary arithmetic. Here all arithmetic is performed relative to the size of base type of pointer.
For example if we have an integer pointer pi which contains address 1000 then on incrementing we get 1004 instead of 1001. This is because the size of int data type is 4. similarly on decrementing pi, we will get 996 instead of 999.
The relational operator ==, !=, <, >, <=, >= can be used with pointer. The operators == and != used to compare two pointer for finding whether they contains same address or not. They will be equal only if both are NULL or they contain address of same variable. The use of these operators is valid between pointer of same type or they contains address of same variable. The use of these operators is valid between pointer of same type or between NULL pointer and any other pointer, or between void pointer and any other pointer.
Pointer to Pointer : Pointer is a variable that can contains memory address. This pointer variable takes some space in which is known as a pointer to pointer variable. Similarly we can have a pointer to pointer variable and this concept can be extended to any limit, but in practice only pointer to pointer is used . Pointer to pointer is generally used while passing pointer variable to function.
Syntax to declaring a pointer to pointer is := data_types **pptr;
Here variable pptr is a pointer to pointer and it can point to a pointer pointing to a variable of type data_type. The asterisk used in the declaration informs the compiler that a pointer to pointer is being declared.