I have a structure with various fields. And there are few global variables. The structure fields and the global variables (basically status-flags) denote same information. It doesnt make sense to have both of them in the RAM area. And i dnt wanna always use the '.' or '->' notations to access the structure fields as there are structure inside the structure.
Hence i feel that a way around is to use unions. But dont know how to use unions for structure in a structure type of variables. can somebody kindly give a small example or a good webpage detailing the same as i couldnt find any relevant thing on web highlighting such an example.
An example would be appreciable. Thank you in advance.
why don't you want to use the . or -> ?
are you afraid your keyboard will wear out ?
you can use macros to get access to them
typedef struct { substruct st1; substruct st2; ... }mainstruct; typedef struct { char flag1; char flag2; char flag3; ... }substruct; //flag1 of st1 can be access in following manner #define MACRO_VARIABLE_1 mainstruct.st1.flag1 #define MACRO_VARIABLE_2 mainstruct.st1.flag2
it probably looks way more complex than it really is.
And i dnt wanna always use the '.' or '->' notations to by which i meant that i would prefer any other method if there possibly exists one.
Thanks for highlighting the usage of macro. but wont prefer it though.
I am of the opinion that there should be some way to achieve this _without bothering the linker_, may be a datatype (i may sound like a jerk, sic). May be this is a languages limitation.
Switch to C++ and use references.
uint8_t &flags = my_struct.silly.flag;
Now you can pretend that flags is just a uint8_t variable and not have to care that in reality it is a masked pointer.
Is there a reason why the language has such a silly limitation?
There's something called aliasing. It seriously breaks a compilers ability to generate optimized code, if there are multiple variables that references the same memory address. A write using one variable will also change the value of the other variable - which is often not possible to see for the compiler.
So the silly thing here is that you want to perform something that the language designers explicitly did not want developers to do.
It may seem silly, but i find that there would be some convenience, Given the scenario where i have to copy the structure from flash memory and then accessing the resources by copying the resources to the variables.
I am just trying to ease my work of remembering each n every members of many different structures and also saving some code involved in copying the struct members into variables.
If i dnt find any alternative, i will readily use one of the two methods (either use of '.' or copy into variables and then access the variables).
Can't use C++ at this moment in the project.
Note that the copy of a struct from flash to RAM doesn't require you to keep track of the contents of the struct.
my_struct_in_ram = my_struct_in_flash;
Aliasing by overlaying variables is probably the most common source of incorrect code generated by compilers - just because they have a very hard time figuring out how a variable assign affects another variable.
And duplicating data manually is a great way to manually produce broken code.
So your choice is to either try to get the compiler to produce incorrect code or to produce incorrect code yourself? Just because you feel it's too much job to write the full access name of the variables? Because you still haven't given a reason why you need to have any flag variables as directly globally accessible without use of the struct addressing.
Modern programming languages works by data hiding and accessor functions. All to help writing software with consistent internal states. And you want to walk the different route. Explicit data duplication.
If students hands in code that intentionally does data duplication then they should get points off for bad solution. And if a developer in a commercial team does it, then he should get a talk with team leader about good and bad software practices.
Workarounds are used when someone is cornered. Only when there are really, really strong reasons to do it. And requiring lots of extra documentation to make sure that anyone who later tries to modify the code are extremely well aware about the special requirements. Time hammering text on the keyboard is nothing in the full life cycle of a software. And better variable names and function names reduces the cost of documenting and maintaining. Intentionally adding trap doors to the code is not an acceptable route in the software industry. And right now, your goal is to figure out the most clever trap door.
char *myvar1, *myvar2; // Global, as pointer //flag1 of st1 can be access in following manner myvar1 = &mainstruct.st1.flag1; myvar2 = &mainstruct.st1.flag2; // assuming mainstruct is also global, or adequate scope Access with *myvar1, or *myvar2 if (*myvar2 == 0x12) { // .. }