I'm in need for a little help on how to put 2 bitfield structs on the same adress. It might sound simple but I doesn't get it to work...
A simple act of "struct2 = &struct1" doesn't work. I also tried to give a pointer to struct2 the adres of struct1. But that didn't work either.
Now I've read something about casting but that's hocus pocus to me...
My assembler doesn't generate any errors with the following typecast...
p_struct2 = (struct1*) &struct2
My question... why can't I set the adres directly via the ampersand action ? And second, is there any other way to set the adress of struct2 on the same location of struct1...
if 2 structures are the same type there is no need to type casting and you can assign to pointers to structures and do it like this:
struct1 * a;
struct2 * b;
b=a;
Two objects or variables cannot share the same address for simple reasons. The first piece of code you gave couldn't work, because you're setting an object to a reference pointer. The second piece of code you gave (assuming p_struct2 is a pointer to an object of type struct2) cannot work either. If it did work and I understood everything correctly, then you have an insane compiler.
Also, sourena... your example does not work. The structs, amusingly, are of different types, even though you put your finger on the fact that they need to be the same.
@melshiki
It is perfectly sane... And the reasons given you are correct. One of your compiler's functions is to make sure you only assign things to like things. Therefore sourena's example will fail because he is trying to assign a (struct1*) to a (struct2*).
By explicitly casting, as you did, that tells the compiler something along the lines of: "I am smart, you are stupid, I know what I am doing, do it and don't complain." That is the purpose of such casts.
There is another way to do it: unions.
1 2 3 4 5 6 7 8 9
union union_float_unsigned;
{
float f;
unsignedint n;
};
union_float_unsigned x;
x.f = 3.14592f;
cout << "The bits for the floating point value " << x.f << " is " << hex << x.n << ".\n";
Just remember that direct casting and unions and the like are your way of telling the compiler that you know what you are doing, so you don't want to hear any complaints.
@Albatross
The construct &myvar generates a pointer, not a reference.
But... the explicit type cast invalidates the assignment, because we end up converting struct1* to struct2* (assuming p_struct2 is of type struct2*). If no errors were returned by his/her compiler, then...
A bit more background info... I got two different structures. Each has a different bitfield overlay...
1 2 3 4 5 6 7 8 9 10
struct struct1{
int port1 :1;
int port2: 2;
and so... till port 8.
};
struct struct2{
int error :2;
int ADC :6;
};
So port1 is set on adres 0x00. In the header file, struct1 is allready set on that adres. I'm using an input to switch between two modes, driving the same 8 outputs. That's why I want struct2 on adres 0x00. So there comes a little extra...
1 2
struct2 * p_struct2;
p_struct2 = (struct1*) &struct2; /* converting struct2 values to the adres of struct1 */
Do i get it right now ? Is this the way how adres 0x00 gets loaded into p_struct2 ?
Second, where do you declare the pointer to struct2 ? Is that before "main" or in the beginning of it.
That's exactly what you had before, and this will not work (are you sure you read what we said? That's an explicit type cast). If you want to "fuse" your two structs, change them to classes, use public:, and consider inheritance. http://cplusplus.com/doc/tutorial/inheritance/
You cannot load two objects into the same memory space (well... unless you define a weird memory system that will not be compatible with C++), and 0x00 is protected memory that if written to will cause a Segmentation Fault. Even reading from it might cause it... I dunno...
If you want p_struct2 to point to the location of struct1, a way to do it is: p_struct2 = (struct2*) &struct1;
(Which Duoas actually told you straight, again, did you really read what we wrote?).
You declare the pointer to struct 2 in the line struct2 * p_struct2... sorry, this might be a little humiliating, but you're biting a bit more off than you can chew, and you need to reread the tutorials. Sorry.