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

Warning: cast to/from pointer from/to integer of different size

I'm learning Pthreads. My code executes the way I want it to, I'm able to use it. But it gives me a warning on compilation. I compile using: gcc test.c -o test -pthread with GCC 4.8.1. And I get the warning test.c: In function…
user159
  • 1,343
  • 2
  • 12
  • 20
32
votes
5 answers

What does this mean: a pointer to void will never be equal to another pointer?

One of my friends pointed out from "Understanding and Using C Pointers - Richard Reese, O'Reilly publications" the second bullet point and I wasn't able to explain the first sentence from it. What am I missing? Pointer to void A pointer to void is…
Sreeraj Chundayil
  • 5,548
  • 3
  • 29
  • 68
31
votes
2 answers

Using intptr_t instead of void*?

Is it a good idea to use intptr_t as a general-purpose storage (to hold pointers and integer values) instead of void*? (As seen here: http://www.crystalspace3d.org/docs/online/manual/Api1_005f0-64_002dBit-Portability-Changes.html) For what I've…
Gepard
  • 499
  • 1
  • 4
  • 7
31
votes
4 answers

Converting a void* to a std::string

After perusing the web and messing around myself, I can't seem to convert a void*'s target (which is a string) to a std::string. I've tried using sprintf(buffer, "%p", *((int *)point)); as recommended by this page to get to a C string, but to no…
Lewis
  • 1,310
  • 1
  • 15
  • 28
29
votes
3 answers

Is it undefined behaviour to delete a null void* pointer?

I know that deleteing a null pointer is a no-op: In either alternative, if the value of the operand of delete is the null pointer the operation has no effect. (C++ Standard 5.3.5 [expr.delete] p2) And also that deleting a void* pointer is…
Xeo
  • 129,499
  • 52
  • 291
  • 397
28
votes
5 answers

sizeof void pointer

why is sizeof void pointer 2 ?
alfesani
  • 451
  • 2
  • 6
  • 11
27
votes
4 answers

how to use void ** pointer correctly?

I am trying to use a double void pointer but I am a little bit confused about the usage. I have a struct that contains a void ** array. struct Thing{ void ** array; }; struct Thing * c = malloc (sizeof(struct Thing)); c->array = malloc( 10 *…
in His Steps
  • 3,075
  • 6
  • 30
  • 38
27
votes
2 answers

When is dynamic_cast useful?

5.2.7/7 says something along the lines of: If T is "pointer to cv void", the result is a pointer to the most derived class pointed to by x. What is a good application of this syntax? When should dynamic_cast be used?
David G
  • 94,763
  • 41
  • 167
  • 253
27
votes
3 answers

Printing a void* variable in C

Hi all I want to do a debug with printf. But I don't know how to print the "out" variable. Before the return, I want to print this value, but its type is void* . int hexstr2raw(char *in, void *out) { char c; uint32_t i = 0; uint8_t *b =…
sharkbait
  • 2,980
  • 16
  • 51
  • 89
25
votes
3 answers

ISO C Void * and Function Pointers

While following some tutorials and reading about function pointers I learned that evidently assigning a void pointer to a function pointer in ISO C is undefined, is there any way to resolve the warning I receive during compile time (e.g. a better…
Tomwa ivory
  • 271
  • 1
  • 3
  • 7
24
votes
4 answers

How to cast an integer to void pointer?

While working with Threads in C, I'm facing the warning "warning: cast to pointer from integer of different size" The code is as follows #include #include #include #include void *print(void *id) { int…
Dinesh
  • 16,014
  • 23
  • 80
  • 122
24
votes
2 answers

How do you convert void pointer to char pointer in C

Ok this has been become sooo confusing to me. I just don't know what is wrong with this assignment: void *pa; void *pb; char *ptemp; char *ptemp2; ptemp = (char *)pa; ptemp2 = (char *)pb; Can anyone tell me why I'm getting this error: error:…
Jimmy
  • 887
  • 1
  • 10
  • 24
24
votes
3 answers

warning: pointer of type ‘void *’ used in arithmetic

I am writing and reading registers from a memory map, like this: //READ return *((volatile uint32_t *) ( map + offset )); //WRITE *((volatile uint32_t *) ( map + offset )) = value; However the compiler gives me warnings like this: warning: pointer…
user1876942
  • 1,411
  • 2
  • 20
  • 32
23
votes
1 answer

Why `void* = 0` and `void* = nullptr` makes the difference?

I was playing with SFINAE and found behavior I cannot explain. This compiles fine: template::value>* = nullptr> void foo(Integer) {} template
Mikhail
  • 20,685
  • 7
  • 70
  • 146
23
votes
4 answers

When is uintptr_t preferred over intptr_t?

Given the requirement that I need to store the value of a "generic" pointer in a struct and have no interest in the pointed-at memory itself, I find it more semantically correct to store it as an intptr_t than a void*. The question is whether a…
Johann Gerell
  • 24,991
  • 10
  • 72
  • 122
1
2
3
90 91