Don’t lose your nerve when you forced to work with pointers?
Introduction
Pointer is an interesting feature of C language. It plays a vital role in major C programs. It is not a complicated data structure; instead it is very simple one that helps us in building many blocks of data structure such as linked list, trees and files.
Declaring pointers
Pointers are variables that store address of other variables (memory locations). As pointer always stores address of memory locations and address of a memory location is always an integer, it will be always of the type integer. So, it need not be declared in fact. But, the location which is pointed by the pointer can have any type of data as per its application. So, when you find a statement in C,
char *p;
it means that the memory type of the pointer is being declared; not the pointer exactly. Here, in the above statement, pointer stores an integer address of a character memory location and integer memory required for storing address will be allocated under the name p. When it is being defined (memory allocated in application), the number of bytes required for a character is going to be allocated.
Allocating memory
The allocation of memory to a pointer can be done C using malloc function available in alloc.h header file. This function accepts a parameter of the type integer which is going to be the size of memory required to be allocated in bytes. For example the following statement declares a float pointer and allocates memory required for float data type.
float *f = (float *)malloc(sizeof(float));
The sizeof operator helps us by yielding size of the memory required for the specified data type. The type casting is required here to convert raw memory bytes to float type. The * is essential to enforce the type casting to be applied for memory bytes instead of address returned by the function. In C++ and most of other modern languages such as java and C# the new operator is used to allocate memory instead of a library function. For example, the same statement given above can be rewritten for C++ as,
float *f = new float;
Similarly memory of any other user defined data type can be defined using the above methods. For example, a structure memory can be allocated using the malloc function in C as follows,
struct employee *p=(struct employee *) malloc(sizeof(struct employee));
An object in C++ can be allocated as follows,
MyClass *ref = new MyClass;
Functions & Pointers
A C Function is a piece of code which can be executed independently when it is called by another function. Functions can accept input through parameters passed to it and can return a value as a result. A function can perform any kind of operation such as adding two numbers, finding a position of a number in an array of values, sorting an array of elements etc. For every operation it performs input parameters should be passed from its calling function. There may number of parameters passed as per the requirement. But, only one value can be returned from a function.
In some situations, there may be as need of returning number of parameters to the caller. This is straightaway not possible. But, we can do this by using pointers. Pointers can be declared as parameters of a function and they accept address of another variable. From the caller, we can pass the address of a local variable to a function and it can manipulate the local variables of the caller by referring its memory address.

In the above diagram, variable a is belonged to main function whereas it is being accessed and its value is getting changed by function fun1. This is possible because the parameter (pointer) r is accepting its value as the address of variable a from main, where it is being called.
Following is the function to swap contents of two variables,
Function to swap contents of two variables | void swap(int *x, int *y) { int t; t= *x; *x=*y; *y=t; } |
Actually, the function swap won’t return anything. But by referring the addresses of two variables passed, it is exchanging the content of them using the third variable.
Similarly pointers can be used to pass an array of values to a function.
sort(&b[0])
The above statement really passes the address of the first element of an array b to the function sort. Since, array elements are located in memory at consecutive locations, address of the first element and count of the elements in the array are enough to access all the elements in an array. Size of memory occupied by each element can be obtained by its data type.
Pointer arithmetic
Usually when you add 1 to a number will result in the next number. But, in pointers, the address refers to different number of bytes depending on data type.
In computer memory each byte will be addressed by an integer. For a character location in c, address refers only 1 byte of memory. For an integer, as it occupies 2 bytes (16 bit integer) so an address refers to two bytes. Here the address of the first byte will be considered and the address of the second byte will be omitted.
For example, if a character array is allocated starting from the address 3422, 3422 will be the address of the first character, 3423 will be the second one, etc., but for an integer array, 3422 will be the address of first two bytes ( an integer) and 3424 is the address of second integer and so on.
So, when you add 1 to a character pointer, it actually adds 1 to the address available in the pointer variable. But, for the integer pointer, adding 1 will add 2 to the content. This is to make the pointer to refer next integer location. For a 32 bit floating type pointer adding one to its content makes it to be incremented with 4 and this is called pointer arithmetic.
Arrays & Pointers
As we have already seen, address of the first element of an array can be passed as a parameter to access all of its members. After accessing the memory location of the address passed, if you increment the address by one, you will be able to access the next element in the array. Likewise, the count number of times the address can be incremented by 1 to access all of the array’s members. The following function accepts address of the first element of an integer array, number of elements in the array and finds the sum of all the numbers in the array.
Function to find sum of the elements in an array | int sumofall(int *f, int count) { int sum=0; for(int i=0; i< count; i++) { sum += *f; f++; } return sum; } |