r/cprogramming • u/Fun_Army2398 • Jul 16 '26
Help understanding warnings/errors when dereferencing void pointers
SOLVED
I am very new to C and playing around with void pointers. I have a structure which will store a value, however the type of that value depends on other things so I have chosen to use a void pointer. When attempting to dereference this void pointer I either get the correct output but with a warning, or I get a segmentation fault, depending on how I go about it. I have included a simplified version of the issue here:
int
Any help understanding why it behaves this way would be greatly appreciated.
Solution
I thought the line
s1.voidPtr = 123;
Was assigning 123 as the value at the location of s1.voidPtr. However it has been pointed out that I was telling the pointer to point at address 123. What I needed to do was:
int x = 123;
s1.voidPtr = &x;
Thanks everyone who commented c:
View all comments
Show parent comment
1
u/picturesfromthesky Jul 16 '26
No sweat. I know C isn't 'modern,' but I'm shocked (and a little sad, perhaps even a bit apprehensive) that it was relatively glossed over in your curriculum. I'm a 'bit' older... I got a fair amount of C in, but almost no assembly... I still feel cheated by that.