POINTERS IN C!
A pointer is a variable used for storing the address of a memory location. Every pointer must be declared first before it is used in a program. This is due to the reason that a pointer can point only one type of data, just like a variable can hold only the data of a particular data type. For instance, consider the pointer declaration given below:
int* a;

The main usage of pointers in an OOP language like C++ is to access the memory locations, which are allocated at run-time (i.e. dynamically). The process of allocating memory space for variables or objects during the course of program execution is called dynamic memory allocation. The other usages of pointers are as follows:
- Pointers help us to access array elements directly (using the address of individual array elements)
- Pointers allow us to implement sharing of data (pass by reference) without copying (pass by value) them. This mechanism lets the modification of formal arguments reflect back on the actual arguments in the caller. This allows a tremendous advantage when you are passing around big arrays as arguments to functions.
- Pointers let us pass arrays and strings to functions and
- Pointers obviously give us the ability to implement complex data structures such as Linked List, Trees and Graphs.
Presentation on Introduction to Pointers
Pointer Declaration and Initialization
Pointer Declaration and Initialization
Specifying the data type for the pointer is the first step involved in declaring a pointer variable. Before its declaration, we should determine the type of variable a pointer would point to. The following is the syntax given for declaring a pointer variable:
where,
- DataType could be a primitive data type or user defined data type like Class or Structure
- ptrVar could be any valid C++ variable name.
In pointer declaration, the symbol * (called Asterisk) is used to inform the compiler that the variable declared (ptrVar) is a pointer variable. The pointer so created can hold the address of any variable of the specified type. Some typical pointer declarations are as follows:
int* pmarks;
char* name;
Date* pDate;
The first two declarations declare pointers of primitive data type, and hence the pointer pmarks can point only the variables of type integer and the variable name can point only a character. The third pointer variable is a pointer to a user-defined data type Date. Using it (pDate), we can point a date containing three fields: dd, mm and yy.
A pointer variable refers to a memory location, which contains the address of another memory location. Hence, after the declaration, a pointer must be assigned with an address of a memory location (or variable) so that it will point somewhere in memory. This can be done through code as follows:
int marks, *pmarks;
pmarks = &marks;
In the above example, pmarks is a pointer variable of type integer and marks is an ordinary variable that can store an integer. After the declaration, the address of the variable marks is assigned to the pointer pmarks using the Address Opeartor (&) so that through pointer pmarks we can access the variable marks.
Presentation on Declaring and Using Pointers
Dereferencing of Pointers
Dereferencing is the process of accessing and manipulating the data stored in the memory location pointed by a pointer. This process can be done through the use of Dereferencing operator * (asterisk). The operator * is used for both dereferencing as well as declaration of pointers. Example code for pointer dereferencing is as follows:
int *pmark, mark;
pmark = &mark;
*pmark = 100;
cout<<*pmark;
In this example, the pointer pmark points to the integer variable mark that will hold an integer. Then the variable mark is assigned a value of 100 through the pointer variable pmark and a dereferencing operator. After the assignment of value 100, the same dereferencing mechanism is applied on pointer pmark to get the data of mark for output. Hence, the statement
cout<<*pmark;
has the same effect as in the following statement:
cout<<mark;
This task of accessing the data through pointers is also known as indirect addressing.
Arithmetic Operations on Pointers
While declaring a pointer variable, C insists that the pointer should be specified of a particular data type. For example,
int *pi;
declares a pointer pi as not simply “a pointer”, but “pointer to an integer”. This is required for doing the arithmetic operations on pointers such as incrementing or decrementing the address of the pointer to refer to the next or previous locations of the current memory location indicated by the pointer.
If we consider that pi is a pointer to an integer, then pi + 1 is the pointer to the integer immediately following the integer *pi in memory, pi - 1 is the pointer to the integer immediately preceding *pi, pi + 2 is the pointer to the second integer following *pi, and so on.
Parameter Passing through Pointers
Pointers provide a two way communication between the function caller and the function itself. Generally, parameters are used to pass values to a function as input. But through pointers, we can make use of the parameter for both input and output purposes. This method of parameter passing is called Parameter Passing by Reference (or Pointer). Parameter passing through pointer is achieved by following the steps given below:
- Declare the formal and actual parameters of the function as pointer variables
- While calling the function, pass the address of the actual parameters instead of their contents.
- Any modification done to formal variables will also reflect in the actual parameters, through which multiple output can be obtained (from function execution).
The following example illustrates this:
#include<stdio.h>
void main(){
float a, b;
printf(“Enter two real number : “);
scanf(“%f %f”, &a, &b);
swap(&a, &b);
printf(“After swapping the values are %f and %f “, a, b);
}
void swap(float *pa, float *pb)
{
Float temp = *pa;
*pa = *pb;
*pb = temp;
}