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!
Main.c
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!
Tak for dine kommentarer!
Spørg AI
Spørg AI
Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat
Awesome!
Completion rate improved to 2.63
Void
Stryg for at vise menuen
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!
Main.c
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!
Tak for dine kommentarer!