Pointer overview

Jaihind's Avatar author of Pointer overview
This is an article on Pointer overview in C.
Pointer is a declared like a variable, but it's not an ordinary variable, it's a fancy variable, that can store address of the another variable. Pointer is very much useful for allocating memory at run time , that's dynamic allocation. Pointers are said to "point to" the variable whose reference they store ( * - Dereference operator ).

See the Following Declarations

int *iA; // integer pointer
char *szB; // character pointer
float *fC; // floating pointer

Here iA, szB and fC are pointer variables, the variables capable of holding addresses.

Main features of pointer
  1. To Reduce the wastage of Memory
  2. Increase the Programming Speed
  3. Data manipulations (Linked list , Stack , Queue , trees.., )

int a[]={10,20,30,40,50};
int *ptr=&a[0];

ptr contains base address of array ' a '
base address points first element of an array 'a'

Pointer Supports Dynamic Memory Management

Windows "C "
Code: CPP
int a[10];         
int *p;      
p= (int *)malloc(sizeof(a));                         
Code: CPP
int b[10][10];
int **q;
*q=(int *)malloc(sizeof(b));
Windows "C++"

Code: CPP
int a[10];
int *p=new int[sizeof(a)];
delete []p;
Code: CPP
int b[10][10];
int **q;
*q=new int[sizeof(b)];
delete []*q;
Using the above functions you can effectively manage ( allocate and deallocate ) memory.

Function Pointers

Pointer can also point to a function , This function have addresses .
Code: CPP
int (*Ptr)( );
int Result();
Ptr=Result;  // Assign Address of function
This above declaration Pointer to a Function , No Arguments which returns an integer.

Pointer to a function with arguments .

Code: CPP
int (*PtrToFunction) (int  iX, int  iY);
int SumofIntegers (int iX,int iY);
PtrToFunction = SumofIntegers;
Function Returning Pointers

As the function has a return type it can return an int, float or any other data type. Similarly it can return a pointer .
Code: CPP
int *ptr;
int *MyFun();
Pointer to an Array

We have already seen, Pointer to int / pointer to float, can we also have pointer to an array? The answer is yes. See below.
Code: CPP
int a[][5]={1,2,3,4,5,6,7,8,9,10};
int (*Ptr)[5];
Ptr is a pointer to an array of 5 integers .

Array of Pointers

We know that array of int , array of float and so similarly array of pointers. Array of pointers contains collection of addresses. Let us consider the below example

Code: CPP
int  *array[3];
int i=10,j=20,k=30;
Structure Pointer

We know that pointer pointing to int / char , Similarly Pointer an point to user defined data types like structures / classes. Let us consider the below example

Code: CPP
struct sJames
    int m_iNumber;
    char *m_szName;

struct sJames Obj={7126,"James"};
struct sJames *ptr=&Obj;
Void Pointer

void pointer is something like pointer pointing to nothing and is a generic pointer. It can be type casted to any other data type.

Code: CPP
void *ptr;
int *p;
int a=10;
p=(int *)ptr;
Const Pointer
const variable normally tells the compiler that you should not allow modification of a particular variable in the program.

const char *str1="James"; //String is Constant Pointer is not
char *const str2="James"; // Pointer is Constant String is not

Code: CPP
char a[]= "prasanna";
str1 = a; // ok
cout << str1<<endl;
//str2 = a; // illegal
str1 = &a[0]; // ok
Null Pointer
Null pointer is a pointer points to NULL that's equal to integer zero.

Code: CPP
int  *Null_Ptr;
Null_Ptr = NULL; // null pointer
Without initialization is better than empty initialization.

Memory Leaks

Memory leak is a big problem using pointers , But if we allocate memory for a particular structure / class / single variable, Proper memory De-allocation is necessary.

To avoid memory errors using pointers

1) Memory allocation without de allocation.
2) With out memory allocation , But de allocation.
3) Memory allocation , De allocation , But again unnecessary de allocation.
coderzone's Avatar, Join Date: Jul 2004
Team Leader
Offtopic comment:
I have gone through almost all your articles and I really like your articles.Good luck

Originally Posted by Jaihind
Without initialization is better than empty initialization.
I guess you are saying the opposite. Its always better to initialize to nothing rather than have it pointed to something.
prem_may10's Avatar, Join Date: Sep 2006
Go4Expert Member
I want to print hello world without using semicolon( in the program
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Originally Posted by prem_may10
I want to print hello world without using semicolon( in the program
Prem, Please put your query as a seperate thread in the Queries and discussion forum. Dont ask your queries as a comments to the articles as that may not eventually get you a reply.
aisha.ansari84's Avatar, Join Date: Feb 2008
nice info
rahul.mca2001's Avatar, Join Date: Feb 2008
Ambitious contributor
can we type cast a null pointer