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

Using void pointers for unknown return types in a linked list c++

I'm trying to implement a linked list and the values held by each node need not always be the same type. I've used a void pointer to store the value, but the issue I'm having is retrieving the value from the node. I want to write a single function…
thedude
  • 3
  • 5
-1
votes
1 answer

Pointing an array of string inside a void

I have a little problem passing a pointer inside a void. This void show only the firts position of the array of struct. When the loop go to second position i get "segmentation fault".I tried to show the array inside the main with the same loop and…
Jilz
  • 31
  • 5
-1
votes
1 answer

Function deque to array not working

Hello I have this problem. I made this function: void* deque2array(tDeque * D){ void *arr = NULL; int i; tNodo * aux = D->ppio; for(i=0; i < D->cant; i++){ arr = aux->elem; arr++; aux=aux->sig; } …
Joseph
  • 45
  • 6
-1
votes
2 answers

Passing a void value function as part of a function signature in C

First off I'm primarily a Java programmer, but I've been tasked with doing some network stuff in C. I've got a function with the following signature: foo(int, void (*) (int, char *, int)) It's the void (*) that's throwing me for a loop. This is…
Darakian
  • 619
  • 2
  • 9
  • 22
-1
votes
2 answers

vector int pointer and initialization of

I have this function: void getInput(vector &list) { int qty, large; cout<<"How many random numbers do you wish to have? "; cin>>qty; cout<<"What is the largest number you wish to see? "; cin>>large; …
Jordan Huang
  • 25
  • 1
  • 8
-1
votes
1 answer

checking the elements of a struct through a void pointer in C

I have an assignment about a labyrinth solving algorithm and I used a path tree to solve it, these are my structs: typedef struct node* nodePtr; typedef struct root{ int coordX; int coordY; nodePtr child[4]; } root; typedef struct…
-1
votes
1 answer

How come a pointer returned by malloc doesn't need to be converted before being used, unlike other (void *) pointers

Does the compiler(and here I'm thinking about gcc, but I guess it could be any C compiler) care about where a variable comes from? Why does it differentiate if the pointer comes from malloc? Is it just an optimization used by some compilers or is…
bsky
  • 19,326
  • 49
  • 155
  • 270
-1
votes
2 answers

Bubble Sort using pointers to function

I'm trying to implement a bubble sort in c by using pointers to function but does not work. Can anyone help me? Here is the code: #include #include void bubbleSort(void** base, size_t length, int (*compar)(const void*, const…
marcelo
  • 171
  • 3
  • 8
-1
votes
1 answer

void pointer in function

I got a problem with void pointer in this program (I am sorry for having to bring up the whole bad program...). #include "stdafx.h" void Input_int(int& InputVar, int Min = -2147483647, int Max = 2147483647); void Output_array(void* Array, unsigned…
ntvy95
  • 193
  • 1
  • 2
  • 10
-1
votes
1 answer

Please help me in understanding the above code behaviour.Understanding Void Pointers & double void Pointer Manipulation

Hi All Please spend some time @ looking the code snippet below : #include #include using namespace std; int func2(void* ptr1) { cout << " IN Func2" << endl; if(ptr1) { //i freed this Pointer. …
user2598064
  • 157
  • 1
  • 13
-1
votes
1 answer

error incompatible types when assigning to type from type void * in C

So this is my header file: #define VECTOR_INITIAL_CAPACITY 20 struct _Variable { char *variableName; char *arrayOfElements; int32_t address; }; typedef struct _Variable Variable; struct _VariableVector { int size; // elements full…
letter Q
  • 14,735
  • 33
  • 79
  • 118
-1
votes
3 answers

Concatenate anything in C ansi

I need to create a C-function to concatenate two of any type of data and return the string that is the result of concatenation. I have done this function below, but it does not work. Could somebody help me? // void pointer does not store value, is…
dsbonafe
  • 185
  • 1
  • 4
  • 14
-1
votes
1 answer

casting from void** to int

I have a dynamic 2D array stored in a void** pointer, and I am just wondering how I am supposed to cast/dereference the values so that they can be printed? Here is an example of what I am trying to do: /* Assume that I have a data structure called…
guskenny83
  • 1,321
  • 14
  • 27
-1
votes
1 answer

dereferencing typecasted void *object pointers

With regards to this piece of code: #include class CClass1 { public: void print() { std::cout << "This should print first" << std::endl; } }; class CClass2 { public: void print() { std::cout << "This should…
smac89
  • 39,374
  • 15
  • 132
  • 179
-1
votes
1 answer

Why passing a simple pointer in this simple example will not work

Why modifying ptr has no effect on vector? I'm trying to change value through f function. void f(int *ptr, int size, int value){ ptr=(int* )malloc(sizeof(int)); if(ptr!=NULL){ int i; for(i=0;i
Piotr Wera
  • 49
  • 1
  • 2
  • 7