๐ŸŒ
FAQs.org
faqs.org โ€บ docs โ€บ learnc โ€บ x658.html
5.5. Generic Pointers
When a variable is declared as being a pointer to type void it is known as a generic pointer. Since you cannot have a variable of type void, the pointer will not point to any data and therefore cannot be dereferenced. It is still a pointer though, to use it you just have to cast it to another ...
๐ŸŒ
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. It is declared like a normal pointer variable but using the void keyword as the pointerโ€™s ...
Discussions

How to do generic function pointers?
Don't cast the function pointers. Just use one function type which takes a generic parameter (traditionally a void *, but I prefer a union if that's feasible). The implementation of each function will know what type that parameter is expected to be and will cast it appropriately. More on reddit.com
๐ŸŒ r/C_Programming
13
42
September 19, 2020
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
void pointers
A void* may point to anything you imagine. That's why C APIs are sometimes swimming in void*. You can also cast a function pointer to a void*. In C this is useful for the qsort() function. int compare(const void* obj1, const void* obj2) { const char* t1 = (const char*)obj1; const char* t2 = (const char*)obj2; return strcmp(str1, str2); } const char* array_of_string[] = {"qwe", "asd", "zxc"}; qsort(array_of_string, sizeof(array_of_string)/sizeof(array_of_string), compare); Now the above is fine C, but is terrible C++. In C++ void* is really not that useful. The above sort could be rewritten as: std::array array_of_string{"qwe", "asd", "zxc"}; std::ranges::sort(array_of_string); More on reddit.com
๐ŸŒ r/cpp_questions
30
20
October 13, 2020
Distinguishing types in _Generic
Arrays decay, but pointers to arrays do not. #include #define FOO(X) _Generic((X), int(*)[2]: "2", int(*)[3]: "3") int main() { int bar[2]; printf("%s\n", FOO(&bar)); } More on reddit.com
๐ŸŒ r/C_Programming
14
7
October 20, 2023
๐ŸŒ
Medium
medium.com โ€บ @ayushkapri.richard โ€บ generic-pointers-void-pointers-in-c-c-8541d51bea3e
Generic Pointers (Void Pointers) in C / C++. | by Ayush Kapri | Medium
May 4, 2024 - Generic Pointers (Void Pointers) in C / C++. A void pointer (void *) is a special type of pointer in C and C++ that is used to point to data of unspecified type. It is often used where the type of โ€ฆ
๐ŸŒ
Quora
quora.com โ€บ How-does-a-generic-pointer-work-in-C
How does a generic pointer work in C? - Quora
Answer (1 of 2): A generic pointer or void pointer in C is pointer with no specific type associated with it. It can hold address of any type of data and can be converted to any type of pointer . [code]int int_var = 9; char char_var = 'A'; void *ptr = &int_var; // pointing to int type variable ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c language โ€บ c-pointers
Pointers in C - GeeksforGeeks
... The void pointers is a pointer of type void that does not have an associated data type. It is also known as a generic pointer because it can store the address of any data type.
Published: 2 weeks ago
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ cprogramming โ€บ c_void_pointer.htm
void Pointer in C
A void pointer in C is a type of pointer that is not associated with any data type. A void pointer can hold an address of any type and can be typecasted to any type. They are also called general-purpose or generic pointers.
๐ŸŒ
Reddit
reddit.com โ€บ r/c_programming โ€บ how to do generic function pointers?
r/C_Programming on Reddit: How to do generic function pointers?
September 19, 2020 -

Hi all, I'm working on a little embedded project and am creating a generic api for connected devices. The idea is, each peripheral(a managed device, sensor, component) can have a list of parameters. I've made a parameter struct which I would like to contain a name, variable size, pointer to a 'get' function and pointer to 'set' function.

The problem I have is that I need different types of 'get' and 'set' functions (some parameters may be 8 bit, some floats, etc). In addition to the parameter to get/set, the function also takes a handle to the peripheral. Obviously, the handle type will change between peripheral types, so I need a generic way of casting both the handle and the function type. Essentially, they're all just addresses, right?

Here's what I have so far...

typedef void(*handle_t); /** < TRY: casting generic handle pointer as void pointer
                                        They should both be same size? **/

typedef int (*getParam32)(handle_t, uint32_t *);
typedef int (*getParamFloat)(handle_t, float *);
//etc //
typedef esp_err_t (*setParam8)(handle_t, uint8_t);
typedef esp_err_t (*setParam16)(handle_t, uint16_t);
// etc //

typedef int (*getFunc)(handle_t, void *); // my optimistic idea for generic 'get'
typedef int (*setFunc)(handle_t, uin32_t); // same, assume uint32_t largest varible size

typedef struct parameter
{
    char param_name[32];    /** < parameter name **/
    uint32_t param_id;      /** < parameter unique id **/
    getFunc get;            /** < get function pointer **/
    setFunc set;            /** < set function pointer **/
    param_type_t valueType; /** < size of parameter in bytes **/
} parameter_t;

// calling the get32, for example... //
if(param.valueType == TYPE_U32) {
    uint32_t value;
    (getParam32 *)param.get(deviceHandle, &value);
}

My thoughts are I could cast the getFunc/setFunc back to their specific types depending on param_type_t (which is just an enum of variable types). However, I'm in pretty deep here and have no idea if this is the right way to do things.

If anyone can offer any advice it'd be much appreciated!

๐ŸŒ
Scribd
scribd.com โ€บ document โ€บ 411424543 โ€บ Void-Pointer
Understanding Generic Pointers in C | PDF
Void pointer or 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.
Find elsewhere
๐ŸŒ
Quora
quora.com โ€บ What-is-a-generic-pointer-in-C-How-do-you-use-it
What is a generic pointer in C++? How do you use it? - Quora
Answer: Question: What is a generic pointer in C++? How do you use it? A generic pointer is the same as a void pointer - so letโ€™s say you have a pointer to hold a buffer: [code]void *buffer; [/code]How do you use it? Same as any pointer but with void pointers, you need to cast it to some other ...
๐ŸŒ
CSE Department
cse.poriyaan.in โ€บ topic โ€บ generic-pointers-50376
Generic Pointers - with Example C Programs - CSE Department
Home | Department: CSE Department | Ist Year | Subject: Programming in C | ... 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 be used to ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c language โ€บ void-pointer-c-cpp
void Pointer in C - GeeksforGeeks
A void pointer in C is a special type of pointer that can store the address of any data type. Since it does not have an associated data type, it is often referred to as a generic pointer.
Published: July 4, 2026
๐ŸŒ
Codeforwin
codeforwin.org โ€บ home โ€บ void pointer or generic pointer in c โ€“ use and arithmetic
void pointer or generic pointer in C - use and arithmetic - Codeforwin
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.
๐ŸŒ
HeyCoach Blog
heycoach.in โ€บ blog โ€บ generic-pointers-in-c
Generic Pointers In C
December 29, 2024 - Definition: A generic pointer is declared using the void* type. Flexibility: It can point to any data type, be it an int, char, or even a custom structure.
Top answer
1 of 3
3

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;
}
๐ŸŒ
Ritambhara
ritambhara.in โ€บ generic-pointers-in-c-language
Generic pointers in C language โ€“ Ritambhara Technologies
Prepare to ace your coding and system design interviews with our comprehensive resources and expert guidance! Ritambhara Technologies offers a curated collection of coding challenges, system design tutorials, mock interviews, and behavioral tips to help you stand out.
๐ŸŒ
O'Reilly
oreilly.com โ€บ library โ€บ view โ€บ mastering-algorithms-with โ€บ 1565924533 โ€บ ch02s05.html
2.5. Generic Pointers and Casts - Mastering Algorithms with C [Book]
August 5, 1999 - For example, given a character pointer sptr (a string) and an integer pointer iptr, we are not permitted to assign sptr to iptr or iptr to sptr. However, generic pointers can be set to pointers of any type, and vice versa.
Author: Kyle Loudon
Published: 1999
Pages: 560
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ void pointer in c
Void Pointer in C - Scaler Topics
October 13, 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 ...
๐ŸŒ
Reddit
reddit.com โ€บ r/cpp_questions โ€บ void pointers
r/cpp_questions on Reddit: void pointers
October 13, 2020 -

Hey guys, I am getting familiar with different concepts of C++. I came across void pointers. I know that when we use malloc it returns a void pointer and we need to typecast it. What else is a void pointer used for and how can we create generic functions using void pointer?

๐ŸŒ
Sankalandtech
sankalandtech.com โ€บ Tutorials โ€บ C โ€บ void-pointer-c.html
void Pointers in C programming.
Let understand from the example, ... the program, i.e., it can point to only int type variable. To solve this problem, we use a pointer to void. A pointer to void known as a generic pointer....
๐ŸŒ
Testbook
testbook.com โ€บ home โ€บ gate โ€บ understanding void pointer in c programming - testbook.com
Understanding Void Pointer in C Programming - Testbook.com
In the realm of computer programming, a void pointer, also known as a generic pointer, is a special type of pointer that is devoid of any associated data type. It is capable of pointing to any data location in the storage, meaning it can hold ...