• [C] Trouble with pointers to unions in structs and floats and oh god.
    11 replies, posted
For some reason, my compiler doesn't agree with my union-in-structure format. Here's the code: [cpp] // My structures typedef struct _Variable JazzVariable; typedef union _Value JazzValue; union _Value { char *Char; float Float; }; struct _Variable { char *Name; JazzValue *Value; }; [/cpp][cpp] // Elsewhere.. void Jazz_SetNumber( JazzVariable* Variable, float Number ) { Variable->Value.Float = Number; // This is line 40 } [/cpp] And here's the error I'm getting: [code]jazz.c|40|error: request for member `Float' in something not a structure or union|[/code] I have absolutely no idea why it disagrees with me on this, I've read through tons of pages, documentation of operators and as far as I can tell, I'm doing it right (. for members, -> for pointers), but it simply won't work! The (dot) operator doesn't like me at all. This problem also happened when I changed another member of the Variable struct to regular member instead of a pointer to one. It still wouldn't accept the dot operator. If I use -> instead of . (regardless of whether Float is a pointer or member) it crashes with a segmentation fault. Any ideas? I'm not sure what kind of information you'd need, so please do ask if I left something vital out. Also, I should note that I have removed some of the stuff from the script that isn't related to this problem.
You can't use the dot on a pointer. The reason -> causes a segmentation fault is because you didn't initialize the pointer. Simple as that.
Oh, now I get it, the -> is of course for the struct not the member. Thank you so much, now I can get on with completely useless and less awesome version of Lua.
[QUOTE=Dr Magnusson;16985577]Oh, now I get it, the -> is of course for the struct not the member.[/QUOTE] No, it is for the member, and that member is a pointer, so you need to use the dereference operator (->) to use it. You probably want it like this: [cpp] typedef struct { char *Name; union { char* Char; float Float; } Value; } JazzVariable; void Jazz_SetNumber( JazzVariable* Variable, float Number ) { Variable->Value.Float = Number; } [/cpp]
But the float is not a pointer?
[QUOTE=Dr Magnusson;16985777]But the float is not a pointer?[/QUOTE] And you're not dereferencing the float anywhere, either :)
Then why do I get a compiler error since I'm not dereferencing a non-pointer member?
[QUOTE=Dr Magnusson;16985811]Then why do I get a compiler error since I'm not dereferencing a non-pointer member?[/QUOTE] Because, in your code, you need to dereference both the Variable pointer and the Value pointer: [cpp] Variable->Value->Float = Number; [/cpp]
The line needs to be [code] Variable->Value->Float = number; [/code] Value is a [I]pointer[/I] to a union. You dereference it and access the member with the second -> [editline]03:08PM[/editline] In any case,[code]Object->member[/code] is just shorthand for [code](*Object).member[/code]
Am I correct in using malloc when initializing my JazzVariables? [cpp] JazzInstance *Instance = malloc( sizeof( JazzInstance ) ); // Start setting stuff here [/cpp] I'm asking, cause whenever I attempt to check the ->Type of the variable, I get a segmentation fault again. Structures [cpp] typedef struct _Variable JazzVariable; typedef enum _Type { TYPE_NULL = 0, TYPE_NUMBER = 1, TYPE_STRING = 2, TYPE_CFUNCTION = 3 } JazzType; struct _Variable { char *Name; JazzType Type; JazzValue Value; JazzVariable *Table; }; [/cpp] This causes the fault [cpp] int Jazz_IsString( JazzVariable *Variable ) { return Variable->Type == TYPE_STRING; } [/cpp] And finally where I got the variable from [cpp] JazzVariable *Jazz_NewString( char *String ) { JazzVariable *Variable = malloc( sizeof( JazzVariable ) ); Jazz_SetString( Variable, String ); return Variable; } [/cpp] [B]Edit:[/B] You know what, this very likely has nothing to do with this code at all, seeing as I'm not getting unless under some special circumstances.. Gonna look into this.
Post the code involved.
Problem was that I had to tell my host application to rebuild itself, or else the updated version of the library of course wouldn't get compiled into it :)
Sorry, you need to Log In to post a reply to this thread.