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

book
Void

We've previously encountered void when we looked at functions that don't return anything or just display text on the screen.

Keep in mind that we can't declare a variable of the void type. However, we can create a void* pointer that points to a memory location.

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 us to store the address of any data type in it.

However, there's a catch: we can't dereference a void* pointer.

Note

This limitation stems from the same reason we can't declare variables of the void type.

Perform an explicit type conversion!

c

Main

copy
#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;
}
123456789101112131415161718192021
#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 basic course in C programming! If you're eager to dive deeper into this fascinating and logical programming language, I highly recommend delving into topics like macros, sorting algorithms, and data structures. Additionally, familiarizing yourself with one of the Linux distributions is invaluable for any seasoned C programmer.

Best of luck!

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
ChatGPT

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

We use cookies to make your experience better!
some-alt