Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Understanding the void Pointers | Pointers
C Basics

bookUnderstanding the void Pointers

When you first saw void, it referred to functions that return nothing. You can’t create a variable of type void, but you can declare a void* pointer that holds the address of any data type.

Note
Note

If you've worked through the pointer exercises in the section, you'll know that all pointers occupy 8 bytes. A void* pointer is no different.

A void* pointer isn't tied to any specific data type. This flexibility allows you to store the address of any data type in it. However, there's a catch: you can't dereference a void* pointer.

Note
Note

This limitation stems from the same reason you can't declare variables of the void type. Perform an explicit type conversion!

Main.c

Main.c

copy
1234567891011121314151617181920
#include <stdio.h> int main() { char c = 'F'; int i = 100; double d = 3.15; void* pV; pV = &c; printf("%c \n", *((char*)pV)); pV = &i; printf("%d \n", *((int*)pV)); pV = &d; printf("%.2f \n", *((double*)pV)); return 0; }

Congratulations on completing the C programming basics! To advance further, explore topics like macros, sorting algorithms, and data structures. Learning a Linux distribution will also help you grow as a skilled C programmer.

question mark

How do you retrieve the value of a void pointer?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 6. ChapterΒ 6

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

Can you explain why you can't dereference a void pointer?

What are some common uses for void pointers in C?

Can you give examples of how to cast a void pointer to another type?

bookUnderstanding the void Pointers

Swipe to show menu

When you first saw void, it referred to functions that return nothing. You can’t create a variable of type void, but you can declare a void* pointer that holds the address of any data type.

Note
Note

If you've worked through the pointer exercises in the section, you'll know that all pointers occupy 8 bytes. A void* pointer is no different.

A void* pointer isn't tied to any specific data type. This flexibility allows you to store the address of any data type in it. However, there's a catch: you can't dereference a void* pointer.

Note
Note

This limitation stems from the same reason you can't declare variables of the void type. Perform an explicit type conversion!

Main.c

Main.c

copy
1234567891011121314151617181920
#include <stdio.h> int main() { char c = 'F'; int i = 100; double d = 3.15; void* pV; pV = &c; printf("%c \n", *((char*)pV)); pV = &i; printf("%d \n", *((int*)pV)); pV = &d; printf("%.2f \n", *((double*)pV)); return 0; }

Congratulations on completing the C programming basics! To advance further, explore topics like macros, sorting algorithms, and data structures. Learning a Linux distribution will also help you grow as a skilled C programmer.

question mark

How do you retrieve the value of a void pointer?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 6. ChapterΒ 6
some-alt