Pointer or not?

I requested help with a certain idea of mine on another forum, and they gave me code with this line in it:
*((int*)lulz.data);
The question is, what does it do?
What I thought it meant was a pointer to lulz.data, where lulz.data is defined as an int. But I'm not sure. Can anyone clarify?
It might be better to post your code so we can see what your trying to do with it, but it looks like your trying to dereference lulz.data that you've casted to an int*.
What I'm trying to do is make a dynamic data type that changes type when needed with a function.
Someone on http://forum.palib.info/index.php?topic=6111.msg43109;topicseen#new posted that I should try using a struct and a switch case, but I would like, in layman's terms, what the above line does. Can you explain dereferencing?
//Ignore the weirdness, it's for a LOLCODE project.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
typedef enum {tYARN, tNUMBR, tNUMBAR, tTROOF, tLETTR } tLOL;

typedef struct LOL {
    void *data;
    tLOL type;
};

LOL IT;
/////////////////////////////////////////////////////////////////////////////
#define IZNOWA ,
void MA(LOL &lulz, tLOL newtype)
{
    switch (newtype) {
        case tYARN:
            *((YARN*)lulz.data);
            break;
        case tNUMBR:
            *((NUMBR*)lulz.data);
            break;
        case tNUMBAR:
            *((NUMBAR*)lulz.data);
            break;
        case tTROOF:
            *((TROOF*)lulz.data);
            break;
        case tLETTR:
            *((LETTR*)lulz.data);
            break;
    }
}

I believe it is because the data is a void*, so they need to cast it as an int*, but then the want it dereferenced (to get the int that it is pointing at), which is why there is the * in front of the entire thing.
Last edited on
So, (Warning, Noob question) how do I access it? (I haven't used pointers much.)
Put an integer variable name and an equals sign in front of the line of code you posted in your first post.
1
2
3
int foo;

foo = *((int*)lulz.data);

It is worth your time to check out the C++ Language Tutorial on this site
http://www.cplusplus.com/doc/tutorial/
(particularly the section on Pointers).

Hope this helps.
Topic archived. No new replies allowed.