-2

I have the following code :

#include <stdio.h>

typedef void* myHandle;

int main() {
    
    int a = 1;
    myHandle Handle = &a;

    printf("%d\n",*Handle);
    

    return 0;
}

Question 1: usually, if I have a pointer of type integer, I can use the dereference operator and get the value of the integer variable. However, in this example, I have a void pointer type, how can I dereference it's contents? it gives me a syntax error

Also, I want to use myHandle like how win32 API use it with HANDLE, I'm doing this for the purpose of only learning how windows does it. From what I get, is that HANDLE is used to reference all related resources. I've read win32 API and saw how a function passes a HANDLE variable to append the address of the contents created to the HANDLE which is pretty cool.

So Question 2: : how can I append addresses to my handle and then access them later like how win32 API does?

John Sall
  • 1,027
  • 1
  • 12
  • 25
  • 3
    "_if I have a pointer of type integer, I can use the dereference operator and get the value of the integer variable._" Cast back to `int*` before dereferencing? – Algirdas Preidžius Jan 06 '21 at 11:15
  • 1
    `printf("%d\n",*static_cast(Handle));`? – MikeCAT Jan 06 '21 at 11:15
  • @MikeCAT do you mind explaining how this worked? – John Sall Jan 06 '21 at 11:19
  • 1
    @JohnSall "_do you mind explaining how this worked?_" Do you know what casts are? You can read about `static_cast`, for example, here: https://en.cppreference.com/w/cpp/language/static_cast , or to read about casts in general: https://stackoverflow.com/questions/332030/when-should-static-cast-dynamic-cast-const-cast-and-reinterpret-cast-be-used – Algirdas Preidžius Jan 06 '21 at 11:20
  • Question 2: You can use an address of a structure that holds addresses as your HANDLE. – MikeCAT Jan 06 '21 at 11:22
  • @MikeCAT but I have to determine beforehand the number of resources I'm going to store their addresses. Isn't there away to have it open and add as much as I want? – John Sall Jan 06 '21 at 11:25
  • 1
    Use an address of `std::vector`. – MikeCAT Jan 06 '21 at 11:25
  • @MikeCAT Yeah it will work, I'm looking for a more simpler method with C language – John Sall Jan 06 '21 at 11:26
  • 1
    You can use `realloc()`. – MikeCAT Jan 06 '21 at 11:27
  • 3
    @JohnSall "_I'm looking for a more simpler method with C language_" If you are asking about C, why did you tag this question with C++? – Algirdas Preidžius Jan 06 '21 at 11:27
  • @MikeCAT so first I create a structure and make Handle point to it, and if there're more resource to add, I use realloc? but still, I won't be able to add any more resources to the struct right? – John Sall Jan 06 '21 at 11:35

2 Answers2

3

void * is generally used as a temporary container for pointers, either to conceal the actual type or for flexibility.

For example, a system like Windows may have some particular type it uses for handles, say some type named foo, and the handles are pointers to foo objects, with type foo *. The authors do not want to define that type for you, for various reasons. Maybe they want to be able to change the type in future versions of the software or have different definitions on different hardware. Or they just do not want to reveal its details so that users will (mostly) not poke around it in and break things. So, when they make a handle, they create a foo object and initialize it normally. Then, when they give it to you, as a return from some routine (directly or via some parameter that points to a place to return a handle), they convert it to a void *.

This void * gives you a value you can store and give back to the system later on but that you cannot (properly) modify or use to access the actual foo. When you make calls to various system routines that require a handle, you will pass this void * back, and the routine will convert it to a foo * and then use the foo it points to.

So, for your question of how to use a void * to dereference a variable, the answer is to convert it back to its original type and then use it normally.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
1

To access the value pointed to by a void *, you have to convert the pointer to the correct type, either by using a temporary object:

int *tmp = Handle;
printf( "%d\n", *tmp );

or by using a cast:

printf( "%d\n", *((int *) Handle));

Note that "handle" types are usually not meant to be dereferenced directly. They’re usually just passed to an API that knows what to do with them, like

printf( "%d\n", getIntFrom( Handle ));

where getIntFrom knows how to access an int value through that handle.

John Bode
  • 119,563
  • 19
  • 122
  • 198