Learn Various Types of pointers in C


Types of pointers in c

Pointers are used to hold the address of another variable of the same data type like int, char, array etc. As we know, every variable has a memory location and every memory location has a memory address. You can access the memory address by using the ampersand (&) operator. C program which prints the address of the variable.
#include <stdio.h>
int main()
{
    int a,b; // Declare variable a and b
    printf ("Address of variable a is %u\n", &a);
    printf ("Address of variable b is %u\n",&b);
    return 0;
}

Output
Address of variable a is 2686748
Address of variable b is 2686744


See Also: Function Pointer in C


int q = 10; //declaration and initialization of variable


Types of pointers in C


int *p;       // declaration of pointer variable


Types of pointers in C

p = &q    // initialization of pointer variable 'p'


types of pointers in C


Pointer variable 'p' keep the address of 'q' that is 2000. To access the value of the variable 'q', use *p (asterisk p).


Types of pointers in C

In the previous article, we discussed what is Pointers in C and how to use pointers. In this article, we will discuss various types of pointers in C.


See Also: Double Pointer in C/Pointer to Pointer in C

What is a NULL pointer in C?

This is considered good practice if you assign a null value to pointer variables. When you don't have a valid address to assign to the pointer variables then you can use NULL. A pointer which is initialized with NULL value is known as a NULL pointer in C. 

A NULL pointer is a predefined macro and its value is 0 which defined in the 'stdio.h', 'alloc.h', 'stdlib.h', 'mem.h', 'stddef.h' header files. 

Syntax
<data type> *<Variable_Name> = NULL;
For instance, int *p = NULL;

In the above example, pointer p is an integer type pointer variable which is initialized with NULL and pointer *p is known as a NULL pointer.

char *p = '\0';
float *p = (float *)0;
double *p = NULL;

#include <stdio.h>
int main()
{
int *p=NULL;
if (p!=NULL)
{
printf ("Pointer p is not a NULL Pointer");
}
else
{
printf ("Pointer p is a NULL Pointer");
}
return 0;
}

Output
Pointer p is a NULL pointer

Wild Pointer in C

Those pointer variables which are declared but not initialized with any address is known as a wild pointer. These pointer variables contain garbage value.

Difference between NULL pointer and the wild pointer is that NULL pointer is initialized with zero whereas, a wild pointer is uninitialized and consist garbage value. Below mention, C program which helps you to understand the concept of the wild pointer in C.
#include <stdio.h>
int main()
{
int *a; //Wild Pointer
int b=10; // Initialization of variable b
//After initialization pointer is not a valid pointer
a = &b;
return 0;
}

Void Pointer in C

A void pointer is an untyped pointer i.e. those pointers which are the point to any variable of any data type that is not associated with any particular data type. It can store the address of any data type. This pointer is also known as the generic pointer or general-purpose pointer because it can store the address of any data type.

Declaration of a void pointer is similar to normal pointer only difference is that void keyword is used instead of data type (int, float, char, double etc.)

Syntax
void <name_of_variable>
For instance, void *p;

C program helps you to understand the working of the void pointer in C.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a = 5;
char b = 'd';
void *p = &a; //Void pointer 'p' holds the address the of integer variable 'a'.
p = &b; //Assign the address of character varaiable 'b' to void pointer 'p'
return 0;
}

Dereferencing a Void Pointer in C
Accessing the value of that address which is stored in the pointer variable is called dereferencing. We need a type-casting because the void pointer is not associated with any data type.
#include <stdio.h>
int main ()
{
void *p; // Declare void pointer 'p'
int q =10; //Initialize integer variable 'q'
p = &q; //Assign the address of variable 'q' to the pointer 'p'.
printf ("*p=%d\n", *p);
return 0;
}

Output
When we will compile the above code then we will get an error because in the above tried to dereference the void pointer without type-casting.


Type-cast  the void pointer

#include<stdio.h>
void main ( )
{
int w=41;
float x=49.5;
char y='a';
void *p;
// *((int*)p)) is type-casting of pointer variables
p=&w;
printf ("Integer variable is %d\n",*((int*)p));

p=&x;
printf ("Float variable is %f\n",*((float*)p));

p=&y;
printf ("Character variable is %c\n",*((char*)p));

}

Output
41
49.500000
a

Dangling Pointers in C

When a pointer pointing to freed or deleted memory location then these pointers are known as dangling pointers. These pointers are created when the object is de-allocated or deleted without modifying the value of the pointer. So, the pointer still points to the de-allocated or freed memory location.


See Also: What are Tokens in C?


The three different ways created Dangling pointers in C:
1) De-allocation of memory
2) Function Call
3) Variable goes out of scope

1) De-allocation of memory

//Dangling pointer is created when the pointer 'p' points to the memory after deallocation
#include <stdio.h>
#include <stdlib.h>
int main()
{
    int *p;
    p=(int*)malloc(sizeof(int));
    free(p); //After free call, Pointer 'p' become dangling pointer
    p = NULL; //No more dangling pointer
    return 0;
}

2) Function Call

//If local variable is not static and the pointer 'p' become dangling pointer if it still pointing to local variable.
#include <stdio.h>
int *fun(); //Declaration of function fun()
int main()
{
int *ptr=fun();
printf ("%d\n", *ptr);
return 0;
}
//Definition of function fun()
int *fun()
{
int a =10;
return &a;
}

Output 
Garbage Value

Pointer 'ptr' doesn't become dangling if 'a' is static.

#include <stdio.h>
int *fun(); //Declaration of function fun()
int main()
{
int *ptr=fun();
printf ("%d\n", *ptr);
return 0;
}
//Definition of function fun()
int *fun()
{
static int a =10;
return &a;
}

Output
10

3) Variable goes out of scope

#include <stdio.h>
int main()
{
    int *p;

    //block
    int a =10;
    p = &a;
    }

    {
        printf ("a=%d\n", *p);  //Here, p is a dangling-pointer
    }
    return 0;
}

Avoid the Creation of Dangling Pointer

Generation of Dangling Pointer can be avoided by initializing pointer to NULL after freed or deleted the memory. After assigning NULL to the pointer means pointer not pointing to anything (memory address). Below mention C program helps you to understand that how NULL can avoid dangling pointer.
#include <stdio.h>
#include <stdlib.h>
int main();
{
    int *p; // Declaration of pointer
    int a =10; // Declaration of variable 
    p = &a; //Initialization of pointer
    free(p); // Pointer p become Dangling
    p = NULL;
    return 0;
}

OLD Pointers


Near, far and huge pointers are designed for MS-DOS operating system which is based on the 8085 microprocessor.  These pointers are used only on the 16-bit machine, so, nowadays, they are not used.

Near pointers in C 

Near pointer is used to store 16-bit address on the data segment, code segment or stack segment, only on the 16-bit machine. At a time, it can only access 64Kb data. You can not access near pointer beyond the segment like text and video memory. Size of this pointer is 2 byte. "near" is a keyword which is used to make any pointer as a near pointer.
#include <stdio.h>
#include <stdlib.h>
int main()
{
    int a =10; //declaration of variable
    int near* p; //declaration of near pointer p
    p=&a; //Assign address of a to the pointer p
    printf ("size of near pointer p is %d\n", sizeof(p));
    return 0;
}

Output
4

Far pointers in C 

This pointer is used to store 32-bit data and can access the memory outside the current segment. To use this, the compiler allocates a segment register to store segment address, then another register to store offset within the current segment. Size of this pointer is 4-byte.
#include <stdio.h>
#include <stdlib.h>
int main()
{
    int a =10; //declaration of variable
    int far* p; //declaration of near pointer p
    p=&a; //Assign address of a to the pointer p
    printf ("size of far pointer p is %d\n", sizeof(p));
    return 0;
}

Output
4

Huge pointers in C 

These pointers are of 32-bit and can access the memory outside this segment. In far pointer, a segment is fixed but in the huge pointer, we can increase or decrease the segment portion.
#include <stdio.h>
#include <stdlib.h>
int main()
{
    int a =10; //declaration of variable
    int huge* far* p; //declaration of near pointer p
    p=&a; //Assign address of a to the pointer p
    printf ("size of pointer p is %d %d %d\n", sizeof(p), sizeof(*p),sizeof(**p));
    return 0;
}

Output
4 4 1

Normalization of Huge pointers

Turbo C compiler works on 8085 microprocessor. In 8085 microprocessor, the physical address of memory is 20 bit long. The process to convert 32-bit huge address into 20-bit physical address is known as normalization.




Read Also

Dynamic Memory Allocation in C Using Malloc(), Calloc(), Free() and Realloc()
What is Structure in C?
What are Storage Classes in C?
Learn Various Types of pointers in C Learn Various Types of pointers in C Reviewed by Glory on 11:00 Rating: 5

No comments:

Powered by Blogger.