25

Say I have a void* named ptr. How exactly should I go about using ptr to store an int? Is it enough to write

ptr = (void *)5;

If I want to save the number 5? Or do I have to malloc something to save it?

Michael Küller
  • 3,982
  • 4
  • 22
  • 42
Tim
  • 4,295
  • 9
  • 37
  • 49
  • 3
    Why, for the goodness sake, do you want to do this? – Griwes Oct 19 '11 at 21:30
  • 2
    What do you want to do? We need to see the bigger picture to help you. Now I can only guess that the code you posted is probably not doing what you would expect it to do. – Karel Petranek Oct 19 '11 at 21:31
  • 3
    Do you want a pointer to memory location 5 or to the number 5? There is a big difference between the two. – dbeer Oct 19 '11 at 21:36
  • Yes, what you have there will work just fine. Whether it's a good idea or not depends on what exactly you're trying to do, but there's no problem with that code as it is. – Carl Norum Oct 19 '11 at 21:30
  • The `cast` is sufficient.................. – James Oct 19 '11 at 21:30
  • We do this for passing undefined data to callback functions – Yvain Jan 14 '21 at 04:32

3 Answers3

47

You're casting 5 to be a void pointer and assigning it to ptr.

Now ptr points at the memory address 0x5

If that actually is what you're trying to do .. well, yeah, that works. You ... probably don't want to do that.

When you say "store an int" I'm going to guess you mean you want to actually store the integer value 5 in the memory pointed to by the void*. As long as there was enough memory allocated ( sizeof(int) ) you could do so with casting ...

void *ptr = malloc(sizeof(int));
*((int*)ptr) = 5;

printf("%d\n",*((int*)ptr));
Brian Roach
  • 76,169
  • 12
  • 136
  • 161
  • 10
    I do this all the time, when working with a framework that stores "User Data" as a `void*`, but all I need is an int. – Mooing Duck Oct 19 '11 at 21:41
  • 1
    As long as someone after you doesn't accidentally dereference it later, I agree that works. It just makes me cringe a bit for that reason. – Brian Roach Oct 19 '11 at 21:50
  • And I knew there was something else that was bugging me - it's not portable. http://developer.gnome.org/glib/2.28/glib-Type-Conversion-Macros.html – Brian Roach Oct 19 '11 at 21:51
  • @MooingDuck Ah! That's exactly what I was running into. So it's a proper use case. Thanks. – Andy Oct 11 '14 at 19:57
  • If the OP had to ask whether to use a malloc or not, it means that he needs to use a malloc. One does not simply walk into storing ints in pointer spaces. – DragonLord Jun 07 '16 at 23:28
7

That will work on all platforms/environments where sizeof(void*) >= sizeof(int), which is probably most of them, but I think not all of them. You're not supposed to rely on it.

If you can you should use a union instead:

union {
    void *ptr;
    int i;
};

Then you can be sure there's space to fit either type of data and you don't need a cast. (Just don't try to dereference the pointer while its got non-pointer data in it.)

Alternatively, if the reason you're doing this is that you were using an int to store an address, you should instead use size_t intptr_t so that that's big enough to hold any pointer value on any platform.

Boann
  • 48,794
  • 16
  • 117
  • 146
  • 4
    No, don't use `size_t`. It isn't guaranteed to be able to hold all possible addresses of pointers. `intptr_t` and `uintptr_t` are there for that since C99. In C90 you're out of luck. I can even give you a platform where `size_t` is 16 bits wide and pointers are 32 bits wide: x86 in real mode using memory model `HUGE`, `FAR` and `COMPACT`. `size_t` can be seen as the type used for addressing arrays, nothing more. – Patrick Schlüter Sep 16 '14 at 07:49
0

A pointer always points to a memory address. So if you want to save a variable with pointer, what you wanna save in that pointer is the memory address of your variable.