๐ŸŒ
Codeforwin
codeforwin.org โ€บ home โ€บ void pointer or generic pointer in c โ€“ use and arithmetic
void pointer or generic pointer in C - use and arithmetic
July 20, 2025 - For such situation, you need a pointer that must work with all types. A void pointer is a special pointer that can point to object of any type. A void pointer is typeless pointer also known as generic pointer.
๐ŸŒ
Cquestions
cquestions.com โ€บ 2009 โ€บ 11 โ€บ generic-pointer-in-c-programming.html
C programming Interview questions and answers: Generic pointer in c programming
Literal meaning of generic pointer is a pointer which can point type of data. ... Here ptr is generic pointer. ... Explanation: Size of any type of near pointer in c is two byte.
Discussions

Are void * pointers meant for generic typing in C? - Stack Overflow
I often see void pointers being cast back and forth with other types of pointers and wonder why, I see that malloc() returns a void pointer that needs to be cast before used also. I am very new to... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Concept of void pointer in C programming - Stack Overflow
In C, a void * can be converted to a pointer to an object of a different type without an explicit cast: void abc(void *a, int b) { int *test = a; /* ... */ This doesn't help with writing your function in a more generic way, though. More on stackoverflow.com
๐ŸŒ stackoverflow.com
c - What does void* mean and how to use it? - Stack Overflow
Take a cue from malloc and calloc. The man page goes on to say: "...return a pointer to the allocated memory, which is suitably aligned for any built-in data type." ... A pointer to void is a "generic" pointer type. A void * can be converted to any other pointer type without an explicit cast. More on stackoverflow.com
๐ŸŒ stackoverflow.com
void pointers in generic linked list
void * is a pointer type. You shouldn't use it to store integers. More on reddit.com
๐ŸŒ r/C_Programming
9
2
November 2, 2020
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c language โ€บ void-pointer-c-cpp
void Pointer in C - GeeksforGeeks
void pointers in C are used to implement generic functions in C.
Published ย  October 11, 2024
๐ŸŒ
Javatpoint
javatpoint.com โ€บ void-pointer-in-c
Void Pointer in C - javatpoint
The sizeof() operator is commonly used in C. It determines the size of the expression or the data type specified in the number of char-sized storage units. The sizeof() operator contains a single operand which can be either an expression or a data typecast where the... ... As we know that we can create a pointer of any data type such as int, char, float, we can also create a pointer pointing to a function.
๐ŸŒ
FAQs.org
faqs.org โ€บ docs โ€บ learnc โ€บ x658.html
Generic Pointers
Example 5-3. generic_pointer.c ยท int main() { int i; char c; void *the_data; i = 6; c = 'a'; the_data = &i; printf("the_data points to the integer value %d\n", *(int*) the_data); the_data = &c; printf("the_data now points to the character %c\n", *(char*) the_data); return 0; }
๐ŸŒ
Atnyla
atnyla.com โ€บ tutorial โ€บ void-generic-pointers โ€บ 1 โ€บ 371
Generic Pointers or Void Pointer in C Programming Language | atnyla
March 31, 2019 - It is still a pointer though, to use it you just have to cast it to another kind of pointer first. Hence the term Generic pointer. This is very useful when you want a pointer to point to data of different types at different times. Void pointer is a specific pointer type โ€“ void * โ€“ a pointer that points to some data location in storage, which doesnโ€™t have any specific type.
๐ŸŒ
HackerEarth
hackerearth.com โ€บ practice โ€บ notes โ€บ void-pointer-in-c
void pointer in C | HackerEarth
A void pointer can hold address of any type and can be typcasted to any type. int a = 10; char b = 'x'; void *p = &a; // void pointer holds address of int 'a' p = &b; // void pointer holds address of char 'b'
๐ŸŒ
Quora
quora.com โ€บ What-are-generic-pointers
What are generic pointers? - Quora
Answer (1 of 10): A generic pointer is a pointer variable that has void as its data type. The void pointer, or the generic pointer, is a special type of pointer that can point to variables of any data type.
Find elsewhere
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ void-pointer-in-c
void pointer in C
July 30, 2019 - In C programming, the function malloc() and calloc() return "void *" or generic pointers.
๐ŸŒ
Scaler
scaler.com โ€บ topics โ€บ void-pointer-in-c
Void Pointer in C - Scaler Topics
October 12, 2023 - This means if we declare an int ... of void pointer here is, we declare a pointer to point to void, meaning a generic pointer, a pointer that has no data type associated with its own, which can point to any data type and can store any datatype's address as will be ...
๐ŸŒ
Ritambhara
ritambhara.in โ€บ generic-pointers-in-c-language
Generic pointers in C language โ€“ Ritambhara Technologies
int x = 5; float y = 3.5; void* vp; // GENERIC POINTER vp = &x; // OK vp = &y; // OK ยท The only problem is that a generic pointer cannot be directly dereferenced. We need to typecast it to relevant data type before dereferencing. This is very useful when you want a pointer to point to data of different types at different times.
Top answer
1 of 3
2

The purpose of a void * is to provide a welcome exception to some of C's typing rules. With the exception of void *, you cannot assign a pointer value of one type to an object of a different pointer type without a cast - for example, you cannot write

int p = 10;
double *q = &p; // BZZT - cannot assign an int * value to a double *

When assigning to pointers of different types, you have to explicitly cast to the target type:

int p = 10;
double *q = (double *) &p; // convert the pointer to p to the right type before assigning to q

except for a void *:

int p = 10;
void *q = &p; // no cast required here.

In the old days of K&R C, char * was used as a "generic" pointer type1 - the memory allocation functions malloc/calloc/realloc all returned char *, the callback functions for qsort and bsearch took char * arguments, etc., but because you couldn't directly assign different pointer types, you had to add an explicit cast (if the target wasn't a char *, anyway):

int *mem = (int *) malloc( N * sizeof *mem );

Using explicit casts everywhere was a bit painful.

The 1989/1990 standard (C89/C90) introduced the void data type - it's a data type that cannot store any values. An expression of type void is evaluated only for its side effects (if any)2. A special rule was created for the void * type such that a value of that type can be assigned to/from any other pointer type without need of an explicit cast, which made it the new "generic" pointer type. malloc/calloc/realloc were all changed to return void *, qsort and bsearch callbacks now take void * arguments instead of char *, and now things are a bit cleaner:

int *mem = malloc( sizeof *mem * N );

You cannot dereference a void * - in our example above, where q has type void *, we cannot get at the value of p without a cast:

printf( "p = %d\n", *(int *)q );

Note that C++ is different in this regard - C++ does not treat void * specially, and requires an explicit cast to assign to different pointer types. That's because C++ provides overloading mechanisms that C doesn't.


  1. Every object type should be mappable to an array of char.
  2. In K&R C, all functions had to return a value - if you didn't explicitly type the function, the compiler assumed it returned int. This made it difficult to determine which functions were actually meant to return a value vs. functions that only had side effects. The void type was handy for typing functions that weren't meant to return a value.

2 of 3
2

C suffers from the absence of function overloading. So most C "generic" functions as for example qsort or bsearch use pointers to void * that to be able to deal with objects of different types.

In C you need not to cast a pointer of any type to a pointer of the type void *. And a pointer of any type can be assigned with a pointer of the type void * without casting.

So in C the functions from your code snippet can be rewritten like

void store(struct GenericStruct *strct, int *myarr){
    strct->ptr = myarr;
}

int *load(struct GenericStruct *strct){
    return strct->ptr;
}
๐ŸŒ
Cplusoop
cplusoop.com โ€บ programming-cplus โ€บ module4 โ€บ generic-pointer-type.php
C++ Generic Pointer type (void*)[Memory Allocation]
November 29, 2024 - The keyword void is used as the ... C++, however, is the use of void* as a generic pointer type. A generic pointer can be assigned a pointer value of any type, but it may not be dereferenced....
Top answer
1 of 16
101

Is it possible to dereference the void pointer without type-casting in C programming language...

No, void indicates the absence of type, it is not something you can dereference or assign to.

is there is any way of generalizing a function which can receive pointer and store it in void pointer and by using that void pointer we can make a generalized function..

You cannot just dereference it in a portable way, as it may not be properly aligned. It may be an issue on some architectures like ARM, where pointer to a data type must be aligned at boundary of the size of data type (e.g. pointer to 32-bit integer must be aligned at 4-byte boundary to be dereferenced).

For example, reading uint16_t from void*:

/* may receive wrong value if ptr is not 2-byte aligned */
uint16_t value = *(uint16_t*)ptr;
/* portable way of reading a little-endian value */
uint16_t value = *(uint8_t*)ptr
                | ((*((uint8_t*)ptr+1))<<8);

Also, is pointer arithmetic with void pointers possible...

Pointer arithmetic is not possible on pointers of void due to lack of concrete value underneath the pointer and hence the size.

void* p = ...
void *p2 = p + 1; /* what exactly is the size of void?? */
2 of 16
33

In C, a void * can be converted to a pointer to an object of a different type without an explicit cast:

void abc(void *a, int b)
{
    int *test = a;
    /* ... */

This doesn't help with writing your function in a more generic way, though.

You can't dereference a void * with converting it to a different pointer type as dereferencing a pointer is obtaining the value of the pointed-to object. A naked void is not a valid type so derefencing a void * is not possible.

Pointer arithmetic is about changing pointer values by multiples of the sizeof the pointed-to objects. Again, because void is not a true type, sizeof(void) has no meaning so pointer arithmetic is not valid on void *. (Some implementations allow it, using the equivalent pointer arithmetic for char *.)

๐ŸŒ
Learn C++
learncpp.com โ€บ cpp-tutorial โ€บ void-pointers
19.5 โ€” Void pointers โ€“ Learn C++
July 19, 2007 - The void pointer, also known as the generic pointer, is a special type of pointer that can be pointed at objects of any data type! A void pointer is declared like a normal pointer, using the void keyword as the pointerโ€™s type: void* ptr {}; // ptr is a void pointer ยท A void pointer can point ...
Top answer
1 of 11
253

A pointer to void is a "generic" pointer type. A void * can be converted to any other pointer type without an explicit cast. You cannot dereference a void * or do pointer arithmetic with it; you must convert it to a pointer to a complete data type first.

void * is often used in places where you need to be able to work with different pointer types in the same code. One commonly cited example is the library function qsort:

void qsort(void *base, size_t nmemb, size_t size, 
           int (*compar)(const void *, const void *));

base is the address of an array, nmemb is the number of elements in the array, size is the size of each element, and compar is a pointer to a function that compares two elements of the array. It gets called like so:

int iArr[10];
double dArr[30];
long lArr[50];
...
qsort(iArr, sizeof iArr/sizeof iArr[0], sizeof iArr[0], compareInt);
qsort(dArr, sizeof dArr/sizeof dArr[0], sizeof dArr[0], compareDouble);
qsort(lArr, sizeof lArr/sizeof lArr[0], sizeof lArr[0], compareLong);

The array expressions iArr, dArr, and lArr are implicitly converted from array types to pointer types in the function call, and each is implicitly converted from "pointer to int/double/long" to "pointer to void".

The comparison functions would look something like:

int compareInt(const void *lhs, const void *rhs)
{
  const int *x = lhs;  // convert void * to int * by assignment
  const int *y = rhs;

  if (*x > *y) return 1;
  if (*x == *y) return 0;
  return -1;
}

By accepting void *, qsort can work with arrays of any type.

The disadvantage of using void * is that you throw type safety out the window and into oncoming traffic. There's nothing to protect you from using the wrong comparison routine:

qsort(dArr, sizeof dArr/sizeof dArr[0], sizeof dArr[0], compareInt);

compareInt is expecting its arguments to be pointing to ints, but is actually working with doubles. There's no way to catch this problem at compile time; you'll just wind up with a missorted array.

2 of 11
30

Using a void * means that the function can take a pointer that doesn't need to be a specific type. For example, in socket functions, you have

send(void * pData, int nLength)

this means you can call it in many ways, for example

char * data = "blah";
send(data, strlen(data));

POINT p;
p.x = 1;
p.y = 2;
send(&p, sizeof(POINT));
๐ŸŒ
tekslatetutor
tekslate.com โ€บ home page โ€บ blog โ€บ c language
Types of Pointers in C | Dangling Pointer in C
The void pointer within C is a pointer that is not allied with any data types. This points to some data location within the storage means points to that address of variables. It is also known as a general-purpose pointer.
๐ŸŒ
The Standard - C
iso-9899.info โ€บ wiki โ€บ Generic_Pointer_To_Pointer
Generic Pointer To Pointer - C
November 22, 2017 - The following code sample illustrates a memory allocator function, the only way we could have used the convention of passing our pointer to the function, and assign to it generically, is only if we had such a generic pointer to pointer. #include <stdio.h> #include <stdlib.h> void my_malloc(int **pp, size_t size) { *pp = malloc(size); } int main(void) { int *p; my_malloc(&p, sizeof *p); return 0; }