CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Union of pointer

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Franck26



Joined: 29 Dec 2007
Posts: 122
Location: Ireland

View user's profile Send private message

Union of pointer
PostPosted: Fri Mar 14, 2008 11:18 am     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Mar 14, 2008 11:22 am     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Mar 14, 2008 11:32 am     Reply with quote

Thanks for your answer ckielstra,
You're right, it works fine with a structure
Embarassed

Franck,
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2005 phpBB Group