# Pointer overview

Discussion in 'C' started by Jaihind, Aug 26, 2006.

1. ### JaihindNew Member

Joined:
Aug 4, 2006
Messages:
4
0
Trophy Points:
0
Pointer
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;

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

Pointer Supports Dynamic Memory Management

Windows "C "
Code:
```int a;
int *p;
p= (int *)malloc(sizeof(a));
free(p); ```
Code:
```int b;
int **q;
*q=(int *)malloc(sizeof(b));
free(*p);```
Windows "C++"

Code:
```int a;
int *p=new int[sizeof(a)];
delete []p;```
Code:
```int b;
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:
```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:
```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:
```int *ptr;
int *MyFun();
ptr=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:
```int a[]={1,2,3,4,5,6,7,8,9,10};
int (*Ptr);
Ptr=a;```
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:
```int  *array;
int i=10,j=20,k=30;
array=&i;
array=&j;
array=&k;
```
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:
```struct sJames
{
int m_iNumber;
char *m_szName;
};

struct sJames Obj={7126,"James"};
struct sJames *ptr=&Obj;
cout<<ptr->m_iNumber<<endl;
cout<<ptr->m_szName<<endl;
```
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:
```void *ptr;
int *p;
int a=10;
ptr=&a;
p=(int *)ptr;
cout<<*p<<endl;
```
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

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

Code:
```int  *Null_Ptr;
Null_Ptr = NULL; // null pointer
```
Note:
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.

2. ### coderzoneSuper Moderator

Joined:
Jul 25, 2004
Messages:
736
37
Trophy Points:
28
[COMMENT]I have gone through almost all your articles and I really like your articles.Good luck[/COMMENT]
I guess you are saying the opposite. Its always better to initialize to nothing rather than have it pointed to something.

3. ### prem_may10New Member

Joined:
Sep 28, 2006
Messages:
15
0
Trophy Points:
0
hi,
I want to print hello world without using semicolon( in the program

Joined:
Jul 12, 2004
Messages:
15,332
377
Trophy Points:
83

5. ### aisha.ansari84New Member

Joined:
Feb 13, 2008
Messages:
82
1
Trophy Points:
0
nice info

Joined:
Feb 13, 2008
Messages:
103