Detailed Explanation of Null Pointers in C Language

So far, we have learned that pointers should point to addresses of the same type specified in their declaration. For example, if we declare an int pointer, then this int pointer cannot point to a float variable or other types of variables; it can only point to int type variables. To solve this problem, we can use a null pointer (void pointer). A null pointer represents a generic pointer that can point to any data type. We can assign the address of any data type to a null pointer, and we can assign a null pointer to any type of pointer without any explicit type conversion.

Syntax of Null Pointer

void *pointer_name;

Here is an example of declaring a null pointer:

void *ptr;

In the above declaration, void is the type of the pointer, and ‘ptr’ is the name of the pointer.

Let’s look at some examples:

int i = 9;        // Integer variable initialization
int *p;           // Integer pointer declaration
float *fp;        // Float pointer declaration
void *ptr;        // Null pointer declaration

p = fp;           // Incorrect assignment
fp = &i;          // Incorrect assignment
ptr = p;          // Correct assignment
ptr = fp;         // Correct assignment
ptr = &i;         // Correct assignment

Size of Null Pointer in C

In C, the size of a null pointer is the same as that of a pointer to a character type. From C’s perspective, the representation of a null pointer is the same as that of a pointer to a character type. The size of pointers will vary based on the platform you are using.

👇Click to claim👇
👉C Language Knowledge Collection

Let’s look at the following example:

#include <stdio.h>
int main() {  void *ptr = NULL;        // Null pointer  int *p = NULL;           // Integer pointer  char *cp = NULL;         // Character pointer  float *fp = NULL;        // Float pointer
  // Size of null pointer  printf("Size of null pointer = %d\n\n", sizeof(ptr));  // Size of integer pointer  printf("Size of integer pointer = %d\n\n", sizeof(p));  // Size of character pointer  printf("Size of character pointer = %d\n\n", sizeof(cp));  // Size of float pointer  printf("Size of float pointer = %d\n\n", sizeof(fp));
  return 0;}

Output

Detailed Explanation of Null Pointers in C Language

Advantages of Null Pointers

Here are the advantages of using null pointers:

  • The malloc() and calloc() functions return null pointers, so these functions can be used to allocate memory for any data type.

#include <stdio.h>#include <malloc.h>
int main() {  int a = 90;
  int *x = (int*)malloc(sizeof(int));  x = &a;  printf("Value pointed by x pointer: %d", *x);
  return 0;}

Output

Detailed Explanation of Null Pointers in C Language

  • Null pointers in C can also be used to implement generic functions.

Some important points related to null pointers:

  • Dereferencing a null pointer in C

You cannot dereference a null pointer directly. See the example below:

#include <stdio.h>
int main() {  int a = 90;  void *ptr;  ptr = &a;  printf("Value pointed by ptr pointer: %d", *ptr);
  return 0;}

In the above code, *ptr is a null pointer that points to the integer variable ‘a’. Since we know that a null pointer cannot be dereferenced, the above code will result in a compilation error because we directly printed the value of the variable pointed to by the pointer ‘ptr’.

Output

Detailed Explanation of Null Pointers in C Language

Now, let’s rewrite the above code to eliminate the error.

#include <stdio.h>
int main() {  int a = 90;  void *ptr;  ptr = &a;  printf("Value pointed by ptr pointer: %d", *((int*)ptr));
  return 0;}

In the above code, we convert the null pointer to an integer pointer using the following statement:

(int*)ptr;

Then, we print the value of the variable pointed to by the null pointer ‘ptr’ using the following statement:

*((int*)ptr);

Output

Detailed Explanation of Null Pointers in C Language

  • Performing arithmetic operations on null pointers

In C, you cannot directly perform arithmetic operations on null pointers. We need to perform appropriate type conversions so that arithmetic operations can be applied to null pointers.

See the example below:

#include<stdio.h>
int main() {  float a[4] = {6.1, 2.3, 7.8, 9.0};  void *ptr;  ptr = a;  for (int i = 0; i < 4; i++) {    printf("%f,", *ptr);    ptr = ptr + 1;   // Incorrect operation  }}

The above code shows a compilation error “Invalid use of null expression” because we cannot directly apply arithmetic operations to a null pointer, i.e., ptr = ptr + 1 is incorrect.

Let’s rewrite the above code to eliminate the error.

#include<stdio.h>
int main() {  float a[4] = {6.1, 2.3, 7.8, 9.0};  void *ptr;  ptr = a;  for (int i = 0; i < 4; i++) {    printf("%f,", *((float*)ptr + i));  }}

The above code runs successfully because we applied the appropriate type conversion to the null pointer, i.e., (float*)ptr, and then we applied arithmetic operations to the null pointer, i.e., *((float*)ptr + i).

Output

Detailed Explanation of Null Pointers in C Language

Why Use Null Pointers?

We use null pointers because they have reusability. Null pointers can store objects of any type, and we can retrieve any type of object using the indirection operator and appropriate type conversion.

Understand this through the following example:

#include<stdio.h>
int main() {  int a = 56; // Initialize integer variable 'a'
  float b = 4.5; // Initialize float variable 'b'
  char c = 'k'; // Initialize character variable 'c'
  void *ptr; // Declare null pointer
  // Assign address of variable 'a'
  ptr = &a;  printf("Value of variable 'a': %d\n", *((int*)ptr));  // Assign address of variable 'b'
  ptr = &b;  printf("Value of variable 'b': %f\n", *((float*)ptr));  // Assign address of variable 'c'
  ptr = &c;  printf("Value of variable 'c': %c\n", *((char*)ptr));
  return 0;}

Output

Detailed Explanation of Null Pointers in C Language

Programmer Technical Exchange Group

Scan to join the group, remember to note: city, nickname, and technical direction.



Leave a Comment