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

Noobie void pointer confusion

Sorry for this Noobie question, I just can't make either of the following code work? snippet 1: int main(void) { void *ptr; *ptr = 1; printf("%d", *ptr); return 0; } snippet 2: int main(void) { void *ptr; int a = 1; ptr…
mko
  • 21,334
  • 49
  • 130
  • 191
-2
votes
2 answers

Typecasting (or deference) void * to struct foo*

In api.h typedef void* hidden_my_type; void do_something(my_type x); In core.c struct _my_type { int a; } void do_something(hidden_my_type void_x) { struct *_my_type x = void_x; /*Don't understand is that correct way to do, as I'm getting…
code muncher
  • 1,592
  • 2
  • 27
  • 46
-3
votes
1 answer

Is this conversion valid in C

As direct conversion to void ** of other types is not valid is this conversion valid in C? void swapValues(void **av, void **bv, size_t size) { int *a = *av; int *b = *bv; char array[size]; memcpy(array, a, size); …
0___________
  • 60,014
  • 4
  • 34
  • 74
-3
votes
1 answer

How to pass this test case? Can someone also explain what I was missing?

This is the tester function: intA = intB = 0; printf("TEST-2: "); intA = getIntPositive(NULL); if (intA == TEST_INT) { printf("\n"); } else { printf("\n"); fail++; } This is the function I have written to get…
-3
votes
3 answers

Same address, different values in c via void pointer type casting

#include int main() { double p = 10.3; void *j = &p; *((int*) j) = 2; printf("%i: %p\n", *((int *)j), &p); printf("%i: %p\n", (int)p, &p); return 0; } So apparently, I think this is what happens, and I am sure I am not…
User626468
  • 57
  • 5
-3
votes
2 answers

What does the lines means when a "this" keyword typecast in void pointer

I'm using a C++ code which is coded by someone else. I want to know what is happening in this line of code. tplayer is an array and OnTickContext is a bool variable. tPlayers[i].OnTickContext = (void*)this;
-3
votes
1 answer

return type of void* in c++

Can I declare a return type of void * for a c++ function? I am working in the Linux environment and I want to return a void * for library handle in case of dlopen for shared library.
-3
votes
5 answers

C - Cast void pointer to structure dynamically

I want to cast a pointer to one of two structures depending on the value of a variable. I am trying this but it is not working - struct typeA { int a; int x; } struct typeB { int b; int a; int x; int z; } int…
The Prenx
  • 483
  • 4
  • 15
-3
votes
2 answers

how to return apointer to function in c

float Aco(char** c, int b, char* a) { ...... } float Ma(char** c, int b, char* a) { ...... } float(*pointer)(char** c, int b, char* a); ?????Funk(int size) { switch (startingLetter) { case 'a': …
or vo
  • 11
  • 3
-3
votes
1 answer

Questions about void pointers

I have two questions about void pointers; we have: void * foo=malloc(99) void **bar=(void**)malloc(99); int i=1; bar++; *bar = foo; 1.Is the above equivalent to the following? bar[i++] = foo; If yes it's unexpected because bar++; moves the…
shinzou
  • 5,850
  • 10
  • 60
  • 124
-3
votes
1 answer

void Pointer to a structure causes error 'dereferencing 'void *' pointer'

I try to initialize a queueADT pointer called initAmigo. Apparently I never create one if the structure is not making the pointers for the (void *data) Reasons why I can't put any data in void *data in node structure: Asuumption 1: Take away void…
arrowinfedex
  • 121
  • 1
  • 3
  • 11
-3
votes
4 answers

Pointer to void as an argument in a function with no prototype for variable number of arguments

Say I have a function that should accept any number of parameters, so what im coing here is declaring no prototype, and letting the function to be created when it is called in the code. I am using a pointer to void to receive the random number of…
-3
votes
10 answers

Saving code space by altering a function call in C

I am calling a function that returns a variable through a pointer parameter. I do not care about the return value of this parameter nor do I want to make a dummy variable to pass to the function. For a simple example's sake, let's say the function…
Jeremy
  • 103
  • 12
-4
votes
2 answers

Can't dereference a char* array passed through a function

The code is the following. The reason I'm using void * args is because this function has to be used in a threaded program. It was tested with and without any threads, and it doesn't work in any of both workflows. I'm pretty new to C and this might…
-4
votes
2 answers

Why can void pointers be subtracted but not added?

Why does printf("%ld\n", (void *)0 - (void *)0); compile, but printf("%ld\n", (void *)0 + (void *)0); does not?
Sapphire_Brick
  • 1,560
  • 12
  • 26
1 2 3
90
91