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
1 answer

Type casting a void pointer to an int with insufficient memory allocation

I'm initializing a void pointer with 1 byte of memory and typecasting it to a int pointer and dereferencing it giving it a value 3(which needs 4 bytes) but it is running fine. Shouldn't this result in an error or cause a runtime exception like OOM? …
HarshaNadimpalli
  • 534
  • 4
  • 12
-1
votes
1 answer

Unsure of how to store integers from a file to a struct in c

I created two structs to store values in. struct pair { int x_pos; int y_pos; }; struct coordinates_header { int length; struct pair data[1000]; }; typedef struct coordinates_header coordinates; coordinates *coords; I then try to…
Kayanda
  • 9
  • 3
-1
votes
1 answer

Get values of void pointer to an array

// C lang #include void func (void *ptr) { int *iptr = ptr; printf("{%d, %d, %d}\n", iptr[0], iptr[1], iptr[2]); } int main() { int ary[] = {111, 222, 333}; func(&ary); return 0; } => output: {111, 222, 333} //…
-1
votes
2 answers

passing a function as void * with its arguments as void * as well

I am having difficulty in passing a function as void* with its argument also void*: #include void* hello(void* (*calc)(void *), void* op){ int val = *(int *) op; int* retval = malloc(sizeof(int)); *retval = calc(val); …
sephora
  • 21
  • 1
  • 7
-1
votes
1 answer

How can I assign a struc to a void pointer passed through the paramters?

In my code I have a struc struct Test { int a; int b; char c; }; With my function: int TestFunction(void* ptr){ struct Test test; test.a = 0; test.b = 1; strcpy(c,"hello"); return 0; } Now to link the temp…
chonglawr
  • 201
  • 1
  • 3
  • 15
-1
votes
1 answer

Declaring struct attribute with different type

I have a struct that contain an attribute that can take many types, I want ask about the most appropriate way to declare this attribute. Example: struct { void* pShape; //poiter to the shape that will be casted on *tCircle or *tRectangle int…
fedi
  • 368
  • 3
  • 7
  • 18
-1
votes
1 answer

Void pointers, memory assignment and valgrind

I have this piece of code: void *data = calloc(1, sizeof(char)+sizeof(float)+sizeof(char)); // line 56 *((char *) data) = 'a'; // line 58 *((float *) data + sizeof(char)) = 0.2f; // line 59 *((char *) data + sizeof(float) + sizeof(char)) = 'a'; //…
-1
votes
5 answers

having some trouble in casting void* to back to String

When I try to run the following code, the program crashes: #include #include typedef char* String; int main() { char string1[] = "hello"; void* try = &string1; String try2 = *(String*)try; printf("%s…
Eden_Yosef
  • 45
  • 5
-1
votes
1 answer

Why is the return value of pthread_join() not changing with each call to pthread_join()

This program works fine if the user enters only 1 number on the command line. It will factor out the prime factors and output them to the console just fine. J_10542741@cs3060:~/assn3$ ./assn3 12 12: 2, 2, 3, My problem is when I test it on these…
-1
votes
2 answers

Passing struct as a parameter in place of void*

I have a function that accepts void* as parameter, but I want to use it as if it's a struct. typedef struct struct1 { int val; } struct2; void func1(void* struct3) { printf("%d",struct3->val); } My purpose is for example, if I have a .h…
ndi equals
  • 187
  • 1
  • 9
-1
votes
2 answers

Void Pointers in Red Black Tree

There are two integers x and 7 which are randomly generated integers. The program uses a red black tree member fucntion insert to insert new values into the tree. I'm not understand the arguments of the insert function, more specifically the use of…
Daniel R
  • 103
  • 1
  • 5
-1
votes
1 answer

Please explain void pointers in C

For example passing a char* str to a function that takes a void *ptr I realize that in order to pass this I need to pass as: fnc(&str) and once inside the fnc(void *ptr) function I need to dereference to use it fnc(void *ptr){ *(char **)ptr =…
John Crash
  • 21
  • 5
-1
votes
1 answer

Copying Int Array Stored at Void Pointer to Int Array

In the below loop, I am attempting to copy data stored at the address of voidPtr. The data here is known to be an integer array of length count. I've read that casting this array to an int and then performing the copy (as shown below) should…
mongolol
  • 941
  • 1
  • 13
  • 31
-1
votes
1 answer

C Linked List segmentation fault 11

I keep getting this Segmentation Fault: 11 error and I don't know why. My Code: typedef struct Node* NodePtr; struct Node { NodePtr next; void *val; }; struct List { NodePtr head; }; typedef struct List* ListPtr; int compare(void…
neby
  • 103
  • 1
  • 2
  • 9
-1
votes
2 answers

Understanding void pointers and typedef pointer functions

I am trying to learn about void pointers and functions that have a typedef (in C). I can't seem to grasp the concept. I have this simple code: #include typedef int (*CompareFunc)(void*, void*); int compareints(void *a, void *b) { …
o.o
  • 3,563
  • 9
  • 42
  • 71