Disadvantages of pointers and errors while using pointers

Here are some of the cons and common errors of pointers:

1) When a pointer goes wrong it is very difficult to find the bug in the program. The reason behind that is a pointer is connected to the rest of the program as it contains the memory locations. That means the whole program gets jeopardized.

2) Uninitialized pointer: For Example:

int a, *b;

a = 25;

*b = a;

In the above statements the pointer b has not been given a value. So, when *b =a happens a is pointed to an unknown memory location.

3) Example:

int a, *b;

a = 25;

b = a;

printf(“%d”, *b);

Here, the assignment b=a is redundant because b gets a value of 25. But it is a pointer and it should have a memory location and not a value. To fix this, we can use: b= &a;

4) Example:

char a[100], b[100];

char *c, *d;

c = a;

d = b;

if(c > d)

A comparison between pointers which do not point to a common object is redundant.

5) Example:

int a[6], b[6];

int *c, d;

c = a;

for(d=0; d<15; ++d) *c++ = d;

Here indexing of two adjacent arrays is done redundantly.

Leave a Reply