Friday, August 17, 2012

Pointer In C (In Depth) (Final) Part 4

Generic Pointer in C Programming

Generic pointer:
void pointer in c is known as generic pointer. Literal meaning of generic pointer is a pointer which can point type of data.

Example:

void *ptr;

Here ptr is generic pointer.


Important points about generic pointer in c?

1. We cannot dereference generic pointer.

         #include "malloc.h"
         void main()
        {
          void *ptr;
          printf("%d",*ptr);
        }
       Output: Compiler error

2. We can find the size of generic pointer using sizeof operator.
   
       #include "string.h"
       void main()
       {
     void *ptr;
     printf("%d",sizeof(ptr));
       }
       Output: 2
       Explanation: Size of any type of near pointer in c is two byte.

3. Generic pointer can hold any type of pointers like char pointer, struct pointer, array of pointer etc without any typecasting.

Example:
void main(){
char c='A';
int i=4;
void *p;
char *q=&c;
int *r=&i;
p=q;
printf("%c",*(char *)p);
p=r;
printf("%d",*(int *)p);

}
Output: A4

4. Any type of pointer can hold generic pointer without any typecasting.

5. Generic pointers are used when we want to return such pointer which is applicable to all types of pointers. For example return type of malloc function is generic pointer because it can   dynamically allocate the memory space to stores integer, float, structure etc. hence we type cast its return type to appropriate pointer type.

Examples:

char *c;
c=(char *)malloc(sizeof(char));
double *d;
d=(double *)malloc(sizeof(double));
Struct student{
char *name;
int roll;
};
Struct student *stu;
Stu=(struct student *)malloc(sizeof(struct student));

NULL pointer in c programming


NULL pointer:
Literal meaning of NULL pointer is a pointer which is pointing to nothing. NULL pointer points the base address of segment.
Examples of NULL pointer:
1. int *ptr=(char *)0;
2. float *ptr=(float *)0;
3. char *ptr=(char *)0;
4. double *ptr=(double *)0;
5. char *ptr=’\0’;
6. int *ptr=NULL;

What is meaning of NULL?
NULL is macro constant which has been defined in the heard file stdio.h, alloc.h, mem.h, stddef.h and stdlib.h as
#define NULL 0
Examples:
What will be output of following c program?
#include "stdio.h"
void main(){
if(!NULL)
printf("I know preprocessor");
else
printf("I don't know preprocessor");
}

Output: I know preprocessor
Explanation:
!NULL = !0 = 1
In if condition any non zero number mean true.
What will be output of following c program?
#include "stdio.h"
void main(){
int i;
static int count;
for(i=NULL;i<=5;){
count++;
i+=2;
}
printf("%d",count);
}
Output: 3
What will be output of following c program?
#include "stdio.h"
void main(){
#ifndef NULL
#define NULL 5
#endif
printf("%d",NULL+sizeof(NULL));
}
Output: 2
Explanation:
NULL+sizeof(NULL)
=0+sizeoof(0)
=0+2 //size of int data type is two byte.
We cannot copy any thing in the NULL pointer.

Wild pointer in c programming language.


Wild pointer:
A pointer in c which has not been initialized is known as wild pointer.
What will be output of following c program?
void main(){
int *ptr;
printf("%u\n",ptr);
printf("%d",*ptr);
}
Output: Any address
Garbage value
Here ptr is wild pointer because it has not been initialized.
There is difference between the NULL pointer and wild pointer. Null pointer points the base address of segmentwhile wild pointer doesn’t point any specific memory location.

Dangling pointer problem in c programming


Dangling pointer:
If any pointer is pointing the memory address of any variable but after some variable has deleted from that memory location while pointer is still pointing such memory location. Such pointer is known as dangling pointer and this problem is known as dangling pointer problem.
Initially:
For example:
What will be output of following c program?
int *call();
void main(){
int *ptr;
ptr=call();
clrscr();
printf("%d",*ptr);
}
int * call(){
int x=25;
++x;
return &x;
}
Output: Garbage value
Explanation: variable x is local variable. Its scope and lifetime is within the function call hence after returning address of x variable x became dead and pointer is still pointing ptr is still pointing to that location.
In other word we can say a pointer whose pointing object has been deleted is called dangling pointer.