What does void * mean and how to use it?

Today, when I read the code of others, I saw something like void *func(void* i); What does this void* mean here for the function name and for the type of the variable, respectively?

Also, when do we need to use this pointer and how to use it?

+80
c
Jul 24 '12 at 8:15
source share
11 answers

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

It is used in those places where you need to work with different types of pointers in the same code. One common example is the qsort library function:

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

base is the address of the 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 is called like this:

 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 implicitly converted from array types to pointer types in a function call, and each of them is implicitly converted from a "pointer to int / double / long " to "pointer to void ".

The comparison functions look something like this:

 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 drop type protection from the window and into the oncoming traffic. There is nothing to protect you from the wrong comparison procedure:

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

compareInt expects its arguments to point to int s, but actually works with double s. It is not possible to catch this problem at compile time; you just end up with an incorrectly sorted array.

+104
Jul 24 '12 at 11:15
source share

Using void * means that a function can take a pointer, which should not be a specific type. For example, in socket functions you have

 send(void * pData, int nLength) 

this means that you can call it in various ways, for example

 char * data = "blah"; send(data, strlen(data)); POINT p; px = 1; py = 2; send(&p, sizeof(POINT)); 
+16
Jul 24. '12 at 8:20
source share
 void* 

is a "pointer to memory without specifying what type is stored." You can use, for example, if you want to pass an argument to a function, and this argument can be of several types and in the function you will process each type.

+2
Jul 24 '12 at 8:17
source share

You can look at this article about pointers http://www.cplusplus.com/doc/tutorial/pointers/ and read the chapter: void pointers .

This also works for C.

A pointer type of type void is a special type of pointer. In C ++, void represents the absence of a type, so void pointers are pointers that indicate a value that is not of type (and therefore also of indefinite length and undefined dereference properties).

This allows void pointers to point to any data type, starting with an integer value or float for a character string. But in return, they are a big limitation: the data specified by them cannot be directly dereferenced (which is logical, since we do not have a dereferencing type), and for this reason we will always have to point the address in the void pointer to another type of pointer pointing to a specific data type before dereferencing it.

+2
Jul 24 '12 at 8:19
source share

C is wonderful in this regard. You can say emptiness - this is nothingness void * - that's it (everyone can be)

This is just a tiny * that matters.

Rene pointed this out. Emptiness * is a pointer to some place. What is, how to "interpret", remains for the user.

This is the only way to have opaque types in C. Very vivid examples can be found, for example, in glib or shared data structure libraries. He was very knowledgeable in "C-interfaces and implementations."

I suggest you read the full chapter and try to understand the concept of a "get" pointer.

+2
Jul 24 '12 at 8:23
source share

The void pointer is known as a generic pointer. I would like to explain an example pthread script.

The stream function will have a prototype like

 void *(*start_routine)(void*) 

The pthread API designers considered the argument and returned the values ​​of the thread function. If these things are made generic, we can type cast for void * when sending as an argument. similarly, the return value can be obtained from void * (But I never used the return values ​​from the stream function).

 void *PrintHello(void *threadid) { long tid; // ***Arg sent in main is retrieved *** tid = (long)threadid; printf("Hello World! It me, thread #%ld!\n", tid); pthread_exit(NULL); } int main (int argc, char *argv[]) { pthread_t threads[NUM_THREADS]; int rc; long t; for(t=0; t<NUM_THREADS; t++){ //*** t will be type cast to void* and send as argument. rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t); if (rc){ printf("ERROR; return code from pthread_create() is %d\n", rc); exit(-1); } } /* Last thing that main() should do */ pthread_exit(NULL); } 
+2
Jul 24 2018-12-12T00:
source share

a void* is a pointer, but the type of what it points to is not specified. When you pass a void pointer to a function, you need to know what type it is in order to return it back to this correct type later in the function in order to use it. You will see examples from pthreads that use functions with a prototype in your example, which are used as a thread function. You can then use the void* argument as a pointer to the general data type of your choice, and then apply it back to that type for use in your stream function. You should be careful when using void pointers, although if you don't return to a pointer of your true type, you may run into all the problems.

+1
Jul 24 '12 at 8:21
source share

Standard C11 (n1570) §6.2.2.3 al1 p55 says:

A pointer to void can be converted to a pointer to or from a pointer to any type object. A pointer to any type of object can be converted to a pointer to void and vice versa; The result is compared with the original pointer.

You can use this general pointer to store a pointer to any type of object, but you cannot use ordinary arithmetic operations with it, and you cannot respect it.

+1
Jul 24 '12 at 8:21
source share

The function takes a pointer to an arbitrary type and returns one of them.

0
Jul 24 '12 at 8:18
source share

this means that the pointer you can use this link to get more information about the pointer http://www.cprogramming.com/tutorial/c/lesson6.html

0
Jul 24 '12 at 8:21
source share

The VOID value before the function name means that it returns nothing. Just do something. On the other hand, VOID as a parameter makes it a general function that can accept any type of parameter. But you must provide a function with the size of this parameter.

-four
Jul 24 '12 at 8:17
source share



All Articles