Pointer in C | Basic IT Topic

Pointer in C

Pointer in C

Pointer in C:-

A pointer in C is a variable which refers only address. (address is a location in memory).

Pointer is 4byte because an address is a 4 byte long.

Pointer cannot refers any address beyond 4GB of memory in GCC compiler.

APPLICATION OF THE POINTER:-

It is used for dynamic memory allocation and array implementation.

Function pointer implementation.

Array implementation

Direct address and hardware address.

Random access or direct access address of memory variable.

 

Dangling pointer:-

If a pointer refers unauthenticated address in memory is called  dangling pointer.

#include <stdlib.h>

#include <stdio.h>

int main()

{

    int *p = (int *)malloc(sizeof(int));

    // ptr becomes a dangling pointer

    free(p);

p = NULL;

}

Wild pointer:-

If a pointer does not refer any address in memory called wild pointer.

#include <stdio.h>
int main()

{

    int *p; // wild pointer

    int x = 10;

    // p is not a wild pointer now

    p = &x;

    return 0;

}

Null pointer:-

When the pointer refers the starting address of the memory is zero address is called null pointer.

APPLICATION OF NULL POINTER:-

It represents the last node of a link list.

It represents the stack or queue is empty or not.

It represents heap memory is empty or not.

#include <stdio.h>

int main()

{

    // Null Pointer

    int * = NULL;

     

    printf("The value of pointer is %u", p);

    return 0;

}

D  reference:-

Doing write or read operation using pointer the concept is called D reference.

D reference of a dangling pointer and wild pointer became segmentation from.

Generic pointer:-

Pointer to void type is called generic pointer.

Generic pointer can refer any address but it cannot be difference

#include <stdio.h>

main()

{

int x=10;

void *p=&x;

printf(“%d”,*p);

}

APPLICATION OF GENERIC POINTER:-

Generic pointer is suitable to refer incomplete types(the memory is allocated  by malloc and calloc )

It is suitable to refer VA-list (VA-list is used as the formal parameter of a function).

Near pointer:-

If a pointer refers address the same segment memory is called near pointer.

Far pointer:-

If a pointer refers address in other segments in memory is called far pointer.

basicittopic

basicittopic

A Computer Science Study for IT students and people of IT community