Pointer is an important and powerful feature of C programming which has made the C more user friendly. At the initial condition, it might be difficult to understand the pointer. Pointer is an address variable that contains/holds the address of another variable. The address is the location of another variable in the memory. For example if one variable contains the address of another variable then the first variable is said to be point to the second variable. There are many reasons for using the pointer, some of which are as follows:
- A pointer allows passing variables, arrays, functions, strings, and structures as function argument.
- It supports dynamic memory allocation and de-allocation of memory segments.
- With the help of pointer, variable can be swapped without physically moving them.
- It allows establishing links between data elements for complex data structures such as link lists, stacks, queues, trees, and graphs.
- Since pointer is directly related to the address of the variable, so the efficiency or speed of the execution of the program will be increased.
- Pointers provide a way to return multiple data items from a function via function arguments.
- Pointers are closely related with arrays and therefore provide an alternative way to access individual array elements. This means we can use pointer instead of using array in the program.
- The use of pointer reduces the length of program.
- Pointer can return more than one values.
A pointer contains the memory address. Most commonly this is the location of another variable, where it has been stored in memory. If one variable contains the address of another variable then the first variable is said to be pointed to the second one. In C pointers are distinct such as integer pointer, character pointer; floating point number pointer etc. a pointer consists of two parts which are:
- The pointer operator
- The address operator
- The pointer operator:
A pointer operator can be represented by combination of *(asterisk) with variable. For example, if a variable of integer data type and also declared *(asterisk) with another variable. It means the variable is of type “pointer to integer”. In other words, it will be used in the program indirectly to access the value of one or more integer variables.
Declaration of the pointer variable:
For example: int *ptr;
Here prt is pointer which holds the address of an integer data type. All pointer variables must be declared before they are used in the programs like other variables. When a pointer variable is declared, an asterisk sign i.e. * must precede the variable name, this identifies the variable as a pointer.
- Address operator:
An address operator can be represented by a combination of ampersand sign (&) with a pointer variable. For example, if a pointer variable is an integer type and also declared ‘&’ with the pointer variable then it means that the variable is of type “address of”. In other words, it will be used in the programs to indirectly access the value of one or more integer variables. The ‘&’ is unary operator that returns the memory address of its operand. For example:
Here the pointer operator & is an operator that returns the address of the variable following it. Therefore the preceding assignment statement could be verbalized as “in receives the address of ptr”. In the example, &ptr represents the address of the ptr which is assigned to the Add.
The other operator * is the complement of &. It is unary operator that returns the value of the variable that is located at the address that follows.
The operation of * implies to the phrase “at address”. Unfortunately, the symbol * represents both the multiplication sign and the “at address”, and the symbol & represents both bit wise AND and the “address of” sign. When these are used as pointer operators, they have no relationship to the arithmetic operators. That happened to look like the same. Both, the pointer operators & and * have higher precedence over all other operators except the unary minus, with which they have equal precedence.
Assigning the pointer variable
A pointer is a variable data type and hence the general rule to assign its value to the pointer is same as that of any other variable data type.
int *ptr1, *ptr2;
Here the address of ‘a’ i.e. 10 is assigned to the pointer variable ptr.
here *ptr represents the content of the ptr. Hence the content of the pointer variable ptr1 is assigned to the variable b, not the memory address.
Here the address of the ptr2 is assigned to the pointer variable ptr2. the contents of both ptr1 and ptr2 will be same as these two variables hold the same address.
A program to display the contents of the pointer.
printf(“x=%d and ptr=%d”,x,ptr);
printf(“\nx=%d and the contents of the ptr=%d”,*ptr);
A program to assign a character variable to the pointer and to display the contents of the pointer.
x=’c’; /* assignment of character*/
printf(“value of x=%c”,x);
ptintf(“\n Pointer value ptr=%c”,y);
A program to assign the pointer variable to another pointer and display the contents of the pointer variable.