简体   繁体   中英

How to access an attribute into a struct from a void pointer?

I am given a task which i can't see what a function does in detail but i only know what id does generally.

So i have a function, let's call it "get_button", and in the documentation i know that "it returns a pointer to the button struct". I know the button struct, it's something like:

struct button{
    int size;
    char title;
    short keyID;
};

The function is something like:

void * get_button(int ID, int group)

But i can't know what it does.

Now i want to know the title, but playing with the complier i got that i have a void pointer in return. So how i am supposed to use this void pointer? ASAIK i have to use a static_cast to give a reference to it, but in this case is a struct; moreover i can't really understand what casting means and how to make the compiler know that i want a certain struct.

This is a particular case where i need to know how to get a parameter into a struct from a void pointer. I don't know if i should use a static_cast or whatever (so i can't understand the reference to it). Neither i need to know how to use propery a void pointer in this case! I just need to know how i can access the attribute in the struct. I've seen the other answers here and there but i couldn't find something valuable for this case.

i can't really understand what casting means

You answer yourself in your next question:

how to make the compiler know that i want a certain struct.

That's exactly it. Casting means that you tell the compiler, that the object has a specific type.

This is a particular case where i need to know how to get a parameter into a struct from a void pointer.

First, cast the void pointer into the pointer type of the object that it points to: *button . Then you can access the attribute normally, using the operator-> on the pointer of correct type.

I don't know if i should use a static_cast or whatever

When casting a void pointer to another pointer type, you should indeed use static_cast .

void* void_pointer = get_button(ID, group); // get the pointer
button* button_pointer = static_cast<button*>(void_pointer); // cast to the type of the pointed object
int size = button_pointer->size; // access the attribute through the pointer

Here is an example of a plain C old style cast with your struct to a void pointer:

#include <iostream>

using namespace std;

struct button{
    int size;
    char title;
    short keyID;
};

int main()
{
    button b;
    b.size  = 1;
    b.title = 'c';
    b.keyID = 2;
    void *p = &b;
    button *a = (button *)p;
    std::cout << a->size << std::endl;
    std::cout << a->title << std::endl;
    std::cout << a->keyID << std::endl;
    return 0;
}

A pointer is basically an address of where in memory something is, in your case a button struct. That address doesn't change when the type of the pointer is changed to void*, so if your library providing the get_button function guarantees that the returned pointer will in fact point to a valid button struct, you can safely cast it back to a button* in order to use it (think of it as giving the compiler some extra information).

You'd go about it as follows:

void *what_is_returned = get_button();
button *the_button = static_cast<button *>(what_is_returned);
char the_character_you_want = the_button->title;

The -> operator means "dereference and access member", it's a shorthand for dereferencing what the left hand side variable contains (looking up the address, like in *the_button) and then accessing the member (like in (*the_button).title), effectively adding the offset of the title member within the button struct to the button's address and fetching the char from there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM