Created
November 23, 2022 13:44
-
-
Save joegasewicz/5c7c9b29aa840c8318ac19861ed4fc24 to your computer and use it in GitHub Desktop.
Void Pointers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* ====================================================================== */ | |
/* Void Pointers*/ | |
/* ====================================================================== */ | |
// void* absence of a type means you are able to store any type | |
// - Often used as a return type or a parameter type of functions | |
// - You must cast it to a type when you dereference it | |
int i = 10; | |
float f = 2.34; | |
char ch = 'k'; | |
void *vptr = NULL; | |
vptr = &i; | |
printf("Value of i = %d\n", *(int*)vptr); | |
vptr = &f; | |
printf("Value of f = %.2f\n", *(float*)vptr); | |
vptr = &ch; | |
printf("Value of ch = %c\n\n\n", *(char*)vptr); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment