Questions tagged [void-pointers]

A void pointer (void *) in C and C++ is a pointer that points to a memory location with no specified type.

A void pointer (void*) in and is a pointer that points to a memory location with no specified type. A void * is typically used to exchange pointers, where some called function may not care about the type of the argument that it receives — for example, functions which operate on raw memory (such as realloc and bzero) and functions which pass along the pointer without needing to access it (for example, GTK+ callbacks).

In C, the use of void * is considered idiomatic as long as no undefined operations (such as dereferencing it or performing pointer arithmetic on it) are taken on the pointer, as C lacks type-safe generics. Unless working with C libraries, C++ users should avoid void * and use templates instead, as compilers can detect usages of templates that are not type-safe.

Note that in standard C, you cannot perform arithmetic on a void *; the GNU C Compiler allows such arithmetic as a (non-portable) extension.

1365 questions
9
votes
6 answers

How to check if a void* pointer can be safely cast to something else?

Let's say I have this function, which is part of some gui toolkit: typedef struct _My_Struct My_Struct; /* struct ... */ void paint_handler( void* data ) { if ( IS_MY_STRUCT(data) ) /* <-- can I do something like this? */ { My_Struct*…
ApprenticeHacker
  • 21,351
  • 27
  • 103
  • 153
9
votes
3 answers

Why id is generic pointer?

I want to know why id is a weak reference pointer,how it is able to handle any class type pointer and at run time how can we detect that which type of class pointer is assigned to id.
User97693321
  • 3,336
  • 7
  • 45
  • 69
9
votes
1 answer

Why do compilers behave differently when static_cast(ing) a function to void*?

The following code compiles without any error in VSC++2017 and doesn't compile in gcc 7.3.0 (error: invalid static_cast from type ‘int(int)’ to type ‘void*’ void* p = static_cast(func)) #include int func(int x) { return 2 * x;…
Soulimane Mammar
  • 1,658
  • 12
  • 20
9
votes
4 answers

How to push and pop a void pointer in C

I have this working code: #import #import typedef struct myarray { int len; void* items[]; } MYARRAY; MYARRAY *collection; void mypop(void** val) { puts(collection->items[collection->len]); *val =…
Lance
  • 75,200
  • 93
  • 289
  • 503
9
votes
2 answers

static_cast'd pointer value

In the current draft standard (and C++17), this is written about static_casting a void *: A prvalue of type “pointer to cv1 void” can be converted to a prvalue of type “pointer to cv2 T”, where T is an object type and cv2 is the same…
geza
  • 28,403
  • 6
  • 61
  • 135
9
votes
3 answers

What is the correct way to temporarily cast void* for arithmetic?

I am C novice but been a programmer for some years, so I am trying to learn C by following along Stanford's course from 2008 and doing Assignment 3 on Vectors in C. It's just a generic array basically, so the data is held inside a struct as a void…
Sam Hammamy
  • 10,819
  • 10
  • 56
  • 94
9
votes
1 answer

the proper way to declare C void pointers in Julia

Ok, I originally badly screwed up my formulation of this question (it's more than a year now since I seriously wrote C++ code and I have pretty limited experience with pure C), so let's try again. Some C code is written to expect you to do something…
9
votes
5 answers

Equivalent to window.setTimeout() for C++

In javascript there's this sweet, sweet function window.setTimeout( func, 1000 ) ; which will asynchronously invoke func after 1000 ms. I want to do something similar in C++ (without multithreading), so I put together a sample loop like: …
bobobobo
  • 64,917
  • 62
  • 258
  • 363
9
votes
2 answers

ios swift CMutableVoidPointer not recognized in observeValueForKeyPath

I'm trying to make use of the NSObject(NSKeyValueObserving) in my Swift class but I'm running into a type problem. Xcode is complaining that it doesn't understand the CMutableVoidPointer type for the 'context' argument in the following…
samonderous
  • 649
  • 1
  • 10
  • 22
9
votes
3 answers

multiple inheritance: unexpected result after cast from void * to 2nd base class

My program needs to make use of void* in order to transport data or objects in dynamic invocation situation, so that it can reference data of arbitrary types, even primitive types. However, I recently discovered that the process of down-casting…
André Pareis
  • 1,084
  • 8
  • 17
9
votes
1 answer

casting void** to 2D array of int - C

i am trying to cast a void** pointer to an int** 2D array in C here is the code that i am trying to work with (with all the extraneous bits removed): \*assume that i have a data structure called graph with some *element "void** graph" in it and…
guskenny83
  • 1,321
  • 14
  • 27
9
votes
7 answers

Passing Void type parameter in C

Hello there I am working on an assignment in C where I need to pass in an unknown type of parameter into a function. For example suppose I have the following: int changeCount(void* element) { element.Count = element.Count++; return…
Adam Lee
  • 2,983
  • 7
  • 35
  • 47
9
votes
2 answers

void* is literally float, how to cast?

So I'm using this C library in my C++ app, and one of the functions returns a void*. Now I'm not the sharpest with pure C, but have heard that a void* can be cast to pretty much any other *-type. I also know that I expect a float at the end…
xNidhogg
  • 331
  • 1
  • 4
  • 12
8
votes
3 answers

C# and void pointers

I am writing my first C# application, but as luck would have it I must use void pointers (working with a DLL which returns handles). From what I read there are a few options: Unsafe code, for example see…
Digital Da
  • 891
  • 1
  • 10
  • 23
8
votes
3 answers

Is it Legal to reinterpret_cast to a void*

I was looking at https://en.cppreference.com/w/cpp/language/reinterpret_cast and I noticed that it specifies the legal types we can always cast to: byte* char* unsigned char* But I did not see void* in the list. Is this an oversight? My use case…
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288