Void Pointer in C: Syntax, Working & Limitations

Jasmine Grover logo

Jasmine Grover Content Strategy Manager

Content Strategy Manager

In C, pointers are variables that point to another variable's memory location. This means that it points to a variable's address. 

  • A different term for it is the general-purpose pointer. 
  • The C methods malloc() and calloc() return void * or generic pointers, respectively.
  • There are multiple different kinds of pointers in the C programming language, each with particular uses. 

Key Terms: Void, Pointers, malloc(), calloc(), Variable, Generic pointers, Syntax, C Language


What is a Void Pointer in C?

[Click Here for Sample Questions]

Void pointers are like pointers without a specific data type. As their name implies, they signal that the pointer is essentially blank, allowing it to store addresses of any data type within a program. 

  • Consequently, these void pointers, holding addresses, can be effortlessly converted into various other data types.
  • This convenience extends to memory allocation as well. 
  • Void pointers make functions flexible enough to allocate bytes and memory as needed. 
  • Let's delve into the syntax of using a void pointer in the C programming language.

Syntax of the Void Pointer in C

[Click Here for Sample Questions]

In C programming, we create a void pointer using the unique keyword "void." This keyword basically tells the compiler that the pointer can work with any data type. 

Here's how to declare a void pointer in C:

void *ptr;

This line of code creates what's called a void pointer in C. It's given the name "ptrName." Because it's a void pointer, it's capable of pointing to variables of any data type you can imagine.


Why use a Void Pointer in C?

[Click Here for Sample Questions]

In C, a void pointer, denoted as void *, is a powerful and flexible feature that allows to work with data of unknown or arbitrary types.

  • It's particularly useful in situations where there is a need to create generic functions or data structures 
  • It can work with different data types without knowing their specific type in advance.

Also Read:


Basic Algorithm

[Click Here for Sample Questions]

Here is how a program uses void pointers. Think of this as the program's pseudocode.

Begin

Declaring v of int data type.

Initializing v = 7.

Declaring w of float data type.

Initializing w = 7.6.

Declaring the pointer u as void.

Initializing the u pointer to v.

Print “The Integer variable in the program is equal to = ”.

Printing the value of v using the pointer u.

Initializing the u pointer to w.

Print “The Float variable in the program is equal to = ”.

Printing the value of w using the pointer u.

End.


Solved Examples

[Click Here for Sample Questions]

Example 1

Take a look at the following program in C and find out the output for the same:

#include<stdlib.h>

int main() {

int g = 83;

float h = 4.9;

void *m;

m = &h;

printf(“\nThe Float in this program is = %f”, *( (float*) m) );

m = &g;

printf(“The Integer in this program is = %d”, *( (int*) m) );

return 0;

}

Ans. The Float in the program is = 4.900000

The Integer in the program is = 83

Ques. 

Take a look at the following program in C and find out the output for the same:

#include<stdio.h>

int main()

{

int x = 73;

void *a = &x;

printf(“%d”, *(int *)a);

return 0;

}

Ans. 73

Example 2

Take a look at the following program in C and find out the output for the same:

#include<stdio.h>

void main()

{

char a=’tweepadock’;

char b=’ubbidubbi’;

char c=’skank’;

float x=2.14;

float y=5.6;

float z=9.37;

int p=81;

int q=44;

int q=29;

void *g;

g=&a;

printf(“The character a in this program is = %c”,*((char*)g));

g=&b;

printf(“The character b in this program is = %c”,*((char*)g));

g=&c;

printf(“The character c in this program is = %c”,*((char*)g));

g=&x;

printf(“The float x in this program is = %f”,*((float*)g));

g=&y;

printf(“The float y in this program is = %f”,*((float*)g));

g=&z;

printf(“The float z in this program is = %f”,*((float*)g));

g=&p;

printf(“The integer p in this program is = %d”,*((int*)g));

g=&q;

printf(“The integer q in this program is = %d”,*((int*)g));

g=&r;

printf(“The integer r in this program is = %d”,*((int*)g));

}

Ans. The character a in this program is = tweepadock

The character b in this program is = ubbidubbi

The character c in this program is = skank

The float x in this program is = 2.14

The float y in this program is = 5.6

The float z in this program is = 9.37

The integer p in this program is = 81

The integer q in this program is = 44

The integer r in this program is = 29

Example 3

Input 

#include<stdlib.h>

int main() {

int a = 7;

float b = 7.6;

void *p;

p = &a;

printf("Integer variable is = %d", *( (int*) p) );

p = &b;

printf("\nFloat variable is = %f", *( (float*) p) );

return 0;

}

Ans. Integer variable is = 7

Float variable is = 7.600000


Working of Void Pointer in C

[Click Here for Sample Questions]

In the world of C programming, the void pointer operates by holding the address of a certain spot in memory. However, since it lacks an associated data type, it remains oblivious to the actual content it's pointing at. 

  • Consequently, directly extracting the value from the data it's indicating is not possible with the void pointer in C.
  • To harness the power of a void pointer in C, it needs to be transformed into a specific data type. 
  • This transformation, known as typecasting, aids the compiler in understanding the size and arrangement of the data it's pointing to. 
  • This critical step enables the C void pointer to be dereferenced and put into use.
  • For a clearer grasp, let's see an example. Explore how a void pointer can be utilized in C to allocate memory for an integer.

void *ptr;

int *iptr;

ptr = malloc(sizeof(int));

iptr = (int*)ptr;

  • In the above code snippet,declare:

(i) a void pointer named, “ptr”

(ii) an integer pointer “iptr”.

  • Next, allocate memory using a function called malloc(). 
  • This newly allocated memory's address was then given to a pointer designed for integers, labeled "iptr."
  • To make things work smoothly, transform the void pointer "ptr" into an integer pointer.
  • This transformation occurred using the line "iptr = (int*) ptr."
  • By carrying out this step, the "ptr" pointer was made capable of accessing and fetching the actual value of the integer that was tucked away in the allocated memory.

The Size of Void Pointers in C Language

[Click Here for Sample Questions]

In a program, the size of the void pointer is similar to the size of the C char data type pointer. However, depending on the platform we use it in the code, its size changes drastically.

See an example to understand it better

#include <stdio.h>

int main()

{

int *p = NULL; // the integer pointer in the program

void *ptr = NULL; // the void pointer in the program

float *fp = NULL; // the float pointer in the program

char *cp = NULL; // the character pointer in the program

// the size of the integer pointer in the program

printf(“the size of the integer pointer in the program = %d\n\n”,sizeof(p));

// the size of the void pointer in the program

printf(“the size of the void pointer in the program = %d\n\n”,sizeof(ptr));

// the size of the float pointer in the program

printf(“the size of the float pointer in the program = %d\n\n”,sizeof(fp));

// the size of the character pointer in the program

printf(“the size of the character pointer in the program = %d\n\n”,sizeof(cp));

return 0;

}

The output of the code written above is 

the size of the integer pointer in the program = 8

the size of the void pointer in the program = 8

the size of the float pointer in the program = 8

the size of the character pointer in the program = 8


Limitations of Void Pointer in C

[Click Here for Sample Questions]

The limitations of void pointer are 

  • In C, the Void Pointer cannot be directly dereferenced. 
  • In order to access values stored in the memory address, it must be typecast into other types.
  • In C, void Pointers cannot be used for Pointer arithmetic operations. 
  • They must be converted into an appropriate Data type before any operations can be performed.

Also Read:


Things To Remember

  • Void pointers store memory addresses without a specific data type association.
  • Due to their lack of data type, void pointers cannot directly retrieve the value they point to.
  • To work with a void pointer, it needs to be type casted into a specific data type.
  • Typecasting helps the compiler understand the size and structure of the data pointed to.
  • Once typecasted, a void pointer can be dereferenced and utilised to access the data's value.
  • Converting a void pointer to an integer pointer, like "iptr = (int*) ptr", allows accessing the integer value stored in the allocated memory.
  • In C, void Pointers cannot be used for Pointer arithmetic operations

Sample Questions

Ques. What is a void pointer in C? (1 mark)
a. A pointer that can only point to integers.
b. A pointer that cannot be dereferenced.
c. A pointer that can point to data of any data type.
d. A pointer used exclusively for function pointers.

Ans. c. A pointer that can point to data of any data type.

Explanation- A void pointer (void *) is a generic pointer that can be used to point to data of any data type. It is not limited to a specific type.

Ques. How do you declare a void pointer in C? (1 mark)
a. void ptr;
b. int *ptr;
c. float ptr;
d. void *ptr;

Ans. d. void *ptr;

Explanation- To declare a void pointer in C, you use the void * syntax.

Ques. What is the primary advantage of using void pointers? (1 mark)
a. They are more memory-efficient.
b. They make your code faster.
c. They allow you to work with data of unknown or arbitrary types.
d. They prevent type casting errors.

Ans. c. They allow you to work with data of unknown or arbitrary types.

Explanation- The primary advantage of using void pointers is their ability to work with data of different types, making them useful in scenarios where data types are unknown in advance.

Ques. Which standard library function is commonly used to allocate memory for a void pointer? (1 mark)
a. alloc()
b. malloc()
c. calloc()
d. allocate()

Ans. b. malloc()

Explanation - The malloc() function is commonly used to allocate memory for a void pointer. It returns a void pointer (void *) to the allocated memory block.

Ques. What is a void pointer in C? (1 mark)

Ans. A void pointer is a special type of pointer in C that doesn't have an associated data type. It can hold the memory address of any data type.

Ques. How do void pointers work? (2 marks)

Ans. Void pointers store memory addresses just like regular pointers, but they lack knowledge of the data type they're pointing to. They need to be typecasted to a specific data type before they can be used to access the actual data.

Ques. Why use void pointers? (1 mark)

Ans. Void pointers are particularly useful when dealing with functions or situations where data types may vary. They allow for more flexible and generic code that can handle different data types.

Ques. Can you dereference a void pointer directly? (1 mark)

Ans. No, you cannot directly dereference a void pointer to retrieve the value it points to. It needs to be typecasted to a specific data type first.

Ques. How do you typecast a void pointer? (1 mark)

Ans. To use the data pointed to by a void pointer, you must typecast it to a specific data type. For example: (int*)ptr would typecast a void pointer to an integer pointer.

Ques. Can a void pointer point to any type of data? (1 mark)

Ans. Yes, a void pointer can be used to point to the memory address of any data type, be it an integer, float, character, or any other.

Ques. What's the purpose of typecasting a void pointer? (1 mark)

Ans. Type Casting a void pointer to a specific data type helps the compiler understand how to interpret the data and access its value correctly.

Ques. Are void pointers used in memory allocation? (2 marks)

Ans. Yes, void pointers are often used with memory allocation functions like malloc(). They can hold the addresses of dynamically allocated memory blocks, making memory allocation more versatile.

Ques. How do you declare a void pointer? (1 mark)

Ans. A void pointer is declared using the syntax: void *ptr; where ptr is the name of the pointer.

Ques. Can you perform arithmetic operations on void pointers? (2 marks)

Ans. Arithmetic operations like addition or subtraction aren't allowed directly on void pointers since their data size is unknown. They need to be type casted before performing such operations.

For Latest Updates on Upcoming Board Exams, Click Here: https://t.me/class_10_12_board_updates


Check-Out: 

Comments


No Comments To Show