c void pointer code example

Example 1: void pointer

// A void pointer is a generic pointer, it has no associated type with it.
// A void pointer can hold address of any type and can be typcasted to any type.

void *ptr;	

///// Examples
void *v;
int *i;

int ivar;
char chvar;
float fvar;

v = &ivar; // valid 
v = &chvar; //valid
v = &fvar; // valid
i = &ivar; //valid 
i = &chvar; //invalid 
i = &fvar; //invalid

Example 2: what is void in c

FUNCTION DECLARATION

when void is used as a function return type,
it indicates that the function does not return a value.


POINTER DECLERATION

When void appears in a pointer declaration,
it specifies that the pointer is universal.


FUNCTION PARAMETER(IN C ONLY)
When used in a function's parameter list, 
void indicates that the function takes no parameters.

Tags:

C Example