Hello,
I'm trying to use an union of pointer and I'm very not successful...
Code:
union uPointer
{
unsigned int8* ptr1;
unsigned int8* ptr2;
};
void MainProg()
{
union uPointer uptr;
unsigned int8 Var1;
unsigned int8 Var2;
Var1 = 0;
Var2 = 0;
uptr.ptr1 = &Var1;
uptr.ptr2 = &Var2;
}
I thought that uptr.ptr1 would point on Var1 and uptr.ptr2 on Var2.
But the result is that both of the pointer are on Var2...
Do you have an idea about that?
Thanks for any help.
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
Posted: Fri Mar 14, 2008 11:22 am
In a union all members are overlayed, i.e. they all share the same memory bytes. When you assigned Var2 the other value was overwritten. From your question it looks like you want to use a structure instead. Change you program by replacing 'union' by the word 'struct'.
Franck26
Joined: 29 Dec 2007 Posts: 122 Location: Ireland
Posted: Fri Mar 14, 2008 11:32 am
Thanks for your answer ckielstra,
You're right, it works fine with a structure
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum