Pointers made simple
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.
void main()
{
int a;
a=3;
fun1(&a);
/* … other statements */
}
void fun1(int *r)
{
*r=7;
/* … other statements */
}
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,
int sumofall(int *f, int count)
{
int sum=0;
for(int i=0; i< count; i++)
{
sum += *f;
f++;
}
return sum;
}










Author: Ganesh Kumar (15 Articles)
Ganesh Kumar has qualified with his Masters in Technology with Distinction. His total experience is about 6 years in Development and 2 years in Teaching. Presently he is working for WDC Ltd., Kolkata, India in C#, .Net and SQL Server.
Leave your response!