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
-2
votes
3 answers

Swap on void* array doesn't work correctly

I have an array of complex numbers, and I just want to swap between two elements. However, I want to implement the swap for unknown types (using void*). So I wrote the following code, using the first swap implementation that I saw here: …
Rodrigo
  • 167
  • 2
  • 9
-2
votes
2 answers

Use of void pointer causes compiler warning errors

I am attempting to create a function that assigns memory and checks to see if the memory allocation has been accomplished. To allow any memory type to be assigned I have set the function to accept void like so: foo(void ***memory, size_t…
-2
votes
2 answers

Swap two structure and two int using void pointers in function parameter

In the below code, I am using a single swap function to swap two types of data. But I am getting lots of error. Can anyone help me that where I am doing wrong? #include #include #include //Structure definition which I…
Vivek
  • 207
  • 1
  • 4
  • 17
-2
votes
2 answers

void* is not a pointer to object type - Casting error from void**

I've got a function Quit that is receiving a void** and I need to pass it to a new function who's receiving a DataStructure*. The problem is that the data in ds is replaced with garbage. What can I do ? void Quit(void** DS){ DataStructure*…
R.Gad
  • 11
  • 2
-2
votes
1 answer

SWIG/Python: Passing pointer into function

I basically want to do exactly this: swig: how to pass void* into generic function I seem to be able to get a value that Python can hold, but when I pass it back in, it is deemed null and causes a fail. So I'm thinking I need some kind of PyObject…
Jiminion
  • 5,080
  • 1
  • 31
  • 54
-2
votes
3 answers

How do I reference and dereference different data types from void pointer?

void *ptr; int num = 13; char c = 'q'; Without using struct, is it possible to reference 'num' and 'c' and then deference from the void pointer?
Meehatpa
  • 349
  • 2
  • 10
-2
votes
1 answer

What does void*(*void)(void*) stands for?

And what's the difference between void*(*void)(void*) and void*(*voi)(void*) and when to use it?
Ochmar
  • 60
  • 8
-2
votes
2 answers

Different ways of setting and getting values from void pointer

When going through some of the links about void pointers, I have seen two types when setting and getting values from void pointers, int main() { int i = 5; //This is first way. void *vPtr = (void *) i; //printing value of…
Kiran
  • 747
  • 2
  • 10
  • 35
-2
votes
2 answers

How to transfer elements from a void pointer array allocated in the stack to the heap?

Say I have an array of void pointers void* arr[10]; and I want to transfer that to the heap. I assume I would just allocate a new pointer array using malloc and then start copying individual elements no? void stackTheap(void** arr) { void**…
orange_juice
  • 191
  • 1
  • 11
-2
votes
2 answers

Why does postfix increment not work on a void *ptr, but ptr = ptr + 1 works?

If I do this: int x = 10; void *ptr = &x; ptr++; The line "ptr++" gives an error. However, If instead of "ptr++" I do this: ptr = ptr + 1; It works just fine. What could be the reason?
mb1994
  • 241
  • 3
  • 13
-2
votes
1 answer

using char * in C (GCC)

I have the following code #include #include int main() { char *A, B, C, D; printf("Enter name A:"); scanf("%s", &A); printf("Enter name B:"); scanf("%s", &B); printf("Enter name C:"); scanf("%s", &C); …
Ammar S
  • 91
  • 8
-2
votes
1 answer

Seg Fault when passing strings through void pointers in C

I am relatively new to programming, and this is my first term working in C. So it is entirely possible that this could be a really simple mistake, or it is also possible that the explanation of what I did wrong could go over my head. My program…
-2
votes
1 answer

Why setting void pointer to an address of int pointer in c is ok and the reverse is not?

I have: void *abc; int *abc2; int someInt = 5; int *intPtr = &someInt; abc = intPtr; // This line is ok without casting abc2 = abc; // Here will be an error without casting Why is this?
abcde
  • 23
  • 4
-2
votes
1 answer

Convert void* to stringstream

What is the best way to convert a void* to stringstream? I need to convert incoming curl data to be able to parse it. I have done the following and it appears to work but Im sure there is a better way void ProcessData(void* data, size_t…
Harry Boy
  • 4,159
  • 17
  • 71
  • 122
-2
votes
2 answers

Void pointer conversion error

Here is an except of my code poly.h #include "gf.h" typedef struct polynome { int deg, size; gf_t * coeff; } * poly_t; /* polynomial has coefficients in the finite field */ class Polinomio{ public: int poly_degppf(poly_t…
Juan
  • 2,073
  • 3
  • 22
  • 37