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
-1
votes
4 answers

How could I uncast a void* that I don't know the data type?

I have a structure with two different std::multimap. I would like to do something like this but, how could I uncast a void* that I don't know the data type is? struct Data{ std::multimap> b; …
Norv
  • 13
  • 1
  • 4
-1
votes
1 answer

C++ Segmentation fault while dereferencing a void pointer to a vector

#include #include #include struct STRU_Msg { std::string name; void *vpData; }; class CMSG { public: template int miRegister(std::string name) { STRU_Msg msg; msg.name =…
n33
  • 383
  • 4
  • 13
-1
votes
2 answers

How to cast a void pointers to float to be used in a function in C?

I changed a code so that it could accept floats in a void pointer cast and use it in a function but while it was working with ints before making adjustment to it made the "incompatible types when assigning to type 'float' from type 'void *'" after I…
Siavash
  • 3
  • 2
-1
votes
1 answer

Address of the object - void* and void**

I was trying to get vtable address which is stored in y but I don't understand why x contains a different address. #include class A { public: virtual int f() { return 2; } }; int main() { A obj; …
-1
votes
1 answer

How to get a string from a function which has a void pointer?

When I try to get a string from a function which has type void*, I only get the first character. I tried to copy string manually without using strcpy() it gives me same problem struct arr { int first int last. void **val; }; //I have a…
-1
votes
1 answer

copying void pointer to another void pointer?

I am trying to copy a void pointer to different indexes of another pointer array, things work fine for characters but there appears problem for integer and doubles Here is my strcture: typedef struct vector_struct { size_t e_sz; char e_type; …
Faizan Shah
  • 123
  • 10
-1
votes
2 answers

Initialization of multiple structure having same member name & member count in single function

typedef struct { int data; int size; } s1; typedef struct { char data; int size; } s2; typedef struct { float data; char size; } s3; func(void *p) { /*this should be generic to all structure.*/ /* Need to do for removing…
-1
votes
1 answer

How to write to a void pointer without knowing the Data Type?

I am trying to write the code for a generic Stack using Singly Linked List in C. I am trying to use (void *) as the data type in each of its functions like: node* getNode(void *, size_t); void append(node **, size_t); etc. The structure for each…
FlarrowVerse
  • 197
  • 2
  • 13
-1
votes
2 answers

Is it possible to call a member function with its pointer and a void object pointer?

Is it possible to call a member function with a specific object instance when only owning a void* pointer to the specific instance and a returnType(*function)(parameters) function pointer? #include class testClass { public: int…
ZeroZ30o
  • 375
  • 2
  • 18
-1
votes
3 answers

How to improve performance of a dynamic array implemented with void**?

I need to implemenet a simple dynamic array that can work with any type. Right now my void** implementation is ~50% slower than using int* directly: #define N 1000000000 // Takes ~6 seconds void** a = malloc(sizeof(void*) * N); for (int i =0; i <…
Alex
  • 34,581
  • 26
  • 91
  • 135
-1
votes
2 answers

Parameter passing multiple values using void pointer

I want to pass multiple arguments to a function using a void pointer. void* function(void *params) { //casting pointers //doing something } int main() { int a = 0 int b = 10; char x = 'S'; void function(???); return 0; } I know that I…
Mark
  • 88
  • 2
  • 10
-1
votes
1 answer

Returning pointers back to itself

My coding assignments came with it's header file, meaning we need to use the same data types, and not vary anything. There is a lot of pointers, (mainly a lot of void *). Meaning things are confusing, more than difficult. we have to do a separate…
-1
votes
2 answers

assignment and comparison pointers in c

I am getting an error in my partition function that reads: assignment makes integer from pointer without a cast v= array[low]; comparison between pointer and integer [-Werror] These are the error I'm getting throughout the partition function.…
dylan
  • 17
  • 3
-1
votes
1 answer

pass function returning void * as argument

I wanted to pass a function taking as argument and returning a void * to another function. Right now i have the following code: MyClass *MyClass::bind(std::function fun) { this->value = fun(this->value); return this; } value…
John Doe
  • 1,613
  • 1
  • 17
  • 35
-1
votes
3 answers

converting from void* to int in c++

I am working in a mqtt application and when i receive the information of the payload from the mqtt broker and try to convert it from the void* that is message->payload to an int as signed int var_1=*((int*) message->payload); instead of converting…