What are the uses of NULL pointer and Void pointer

Interview Questions
  • What are the uses of NULL pointer and void pointer.
  • Can pointer arithmetic be applied to void pointers.
  • What is pointer Arithmetic ? What are the valid and Invalid Pointer Arithmetic operations.

What are the uses of NULL pointer and Void pointer

NULL pointer is used to initialize a pointer at the time of declaration if we don't have any explicit value to initialize. It is a good practice to initialize a pointer with NULL to ensure that it is not pointing a random memory location.

A Void pointer is used to point to a variable of any data type. It is a generic pointer, before accessing the data stored at the location pointed by a void pointer we have to typecast it to some particular pointer type.


Can pointer arithmetic be applied to Void pointers

NO, pointer arithmetic cannot be applied to a void pointer because it is a generic pointer, compiler is unaware about the type of data it is pointing to like whether it is char, float or structure.

For Example:
When a character pointer is incremented, its value is increased by 1 that is equal to the size of char data type. A void pointer can point to a variable of any data type of size X, So while incrementing a void pointer compiler don't know how much value to add to current address.


What is pointer Arithmetic ? What are the valid and Invalid Pointer Arithmetic operations.

We can perform arithmetic operations on pointer variable just as you can on numeric value. A pointer in C is a variable which is used to store the memory address which in turn is a numeric value. The arithmetic operations on pointer variable effects the memory address pointed by pointer.

Valid Pointer Arithmetic Operations

  • Adding a number to pointer.
  • Subtracting a number form a pointer.
  • Incrementing a pointer.
  • Decrementing a pointer.
  • Subtracting two pointers.
  • Comparison on two pointers.
Invalid Pointer Arithmetic Operations
  • Addition of two pointers.
  • Division of two pointers.
  • Multiplication of two pointers.


Related Topics
What is Address Of(&) Operator and Value Of(*) Operator in C.
Can array size be declared at run time.
What is Dangling Pointer in C.
What is NULL pointer in C.
What are the different
What is the difference between & and * operators in C
What is the use of goto statement in C
Can we use negative array indexes in C.
What is the difference between strcpy and strncpy function
From which function execution of any C program starts.