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

Pointer arithmetic of unspecified type with known size

void * ptr = NULL; // array of unspecified 13-byte type for (int i = 0; i < 10; i++) { printf("%i ", ((char (*) [13]) ptr) + i); } putchar('\n'); Output: 0 13 26 39 52 65 78 91 104 117 So this works (at least on GCC) but how can I declare a…
Salt
  • 13
  • 2
-4
votes
2 answers

Not Understanding Void Pointer / How to cast? (C Programming)

Suppose I want to write a function: int read_file (char *filename, void *abc) that reads the file, and puts the numbers in an array, which abc points to. I must use the void pointer - I know how I would do it if it were int *abc instead of void,…
Harambe17
  • 175
  • 7
-4
votes
1 answer

How to find a unicode char pointer in a void pointer?

Assume I have the following: wchar_t *x = L"myname"; void *y = 0; // assume that p is already assigned previously to any given buffer How can I determine if the unicode char pointer x is inside the void* y buffer? Basically How can I find a needle…
Farrell32
  • 7
  • 3
-4
votes
2 answers

Usage of void pointers in C++

Is there ever a time they're necessary or are they a symptom of poor code design? If the former, can you give an example? If the latter, can you explain the dangers of using them?
destrovel
  • 75
  • 1
  • 10
-4
votes
2 answers

void / void * declaration behaviour

program 1: int main() { void v=8; printf("v=%d\n",v); } program 2: int main() { void *v=8; printf("*v=%u\n",*v); printf("v=%u\n",v); } compilation error on program 1: **error**: variable or field ‘v’ declared void void…
prashad
  • 107
  • 2
  • 15
-4
votes
2 answers

Void * pointer to a structure

I am implementing a timeout . So i declared a timeout structure typedef struct{ unsigned long * Task; unsigned long Timeout; unsigned long Offset; DATA * Next; DATA * Previous; } TIMEOUT; and i initialized it to : TIMEOUT…
Rad1
  • 185
  • 1
  • 4
  • 17
-4
votes
4 answers

deleting a void pointer using delete operator

whats wrong with the code. What it should be. As it is throwing an error. Operator 'delete', applied to void* argument. int i; void *ptr = &i; delete ptr;
Anand
  • 67
  • 4
  • 9
-4
votes
2 answers

Return a struct pointer in void function

i am new c++ programming and just started with structures and pointers and i got a doubt. i have a struct and void function() struct my_struct { int x; } void my_function(){ my_struct* x=10 } i need to return the value of my_struct* x to…
Rd7
  • 45
  • 2
  • 9
-5
votes
3 answers

Is reinterpret_cast any slower than a static_cast?

I'm comparing two pointers of class typedef value_type which are each of type T* or char16_t. The compiler complains that I can't compare the two because they are distinct types: 'some_type1::value_type*' and…
Zhro
  • 2,546
  • 2
  • 29
  • 39
-5
votes
1 answer

Difference between void * and void **

Other than being able to dereference a void**, I don't understand the following: void * foo, **bar; foo++;//error bar++;//no error Why doesn't the first work but the second does? What's the difference?
shinzou
  • 5,850
  • 10
  • 60
  • 124
-5
votes
2 answers

Does C language specify any implicit initialization for void pointers only?

Here is my code: int main() { int *p; void *x; printf("%p\n", p); printf("%p\n", x); return 0; } which will print: koraytugay$ ./a.out 0x7fff53b35ad0 0x0 koraytugay$ ./a.out 0x7fff5803fad0 0x0 koraytugay$ ./a.out …
Koray Tugay
  • 22,894
  • 45
  • 188
  • 319
-6
votes
4 answers

Convert data from const void *data to double

I received data from some other function to myfunction(const void *data) where pointer data stores the values like {0,0,0,0,0,0,0,40,20,0,0,0,0,0,0}. I want to access just values from {40,20,0,0,0,0,0,0} and convert into a double type value that…
-7
votes
2 answers

C: error: array has incomplete element type 'void'

==> EXTREME BEGINNER QUESTION FOR EXTREME BEGINNERS <== Does anyone know why this: void buf[1]; returns this error: error: array has incomplete element type 'void'. Is it normal ?
AymenTM
  • 529
  • 2
  • 5
  • 17
-12
votes
3 answers

Void Pointer Error

#include func(void *ptr) { *ptr = NULL; } int main() { void *ptr = (int*)malloc(sizeof(int)); func(ptr); return 0; } Can Some one Help me in Resolving this. I want to assign NULL to this Pointer. Do Not make any…
Prag Rao
  • 1,411
  • 3
  • 11
  • 10
1 2 3
90
91