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

Calling with a reference of union

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



Joined: 11 Sep 2007
Posts: 7

View user's profile Send private message

Calling with a reference of union
PostPosted: Wed Oct 03, 2007 3:08 am     Reply with quote

Hi,
I'm trying to create buffer writing function.
Code:

union LCD_BUF
{
    struct
    {
       int1 lobat;
       int1 sign;
       int   other:6;
       int1 dot1;
       int   dig1:7;
       int1 dot2;
       int   dig2:7;
       int1 dot3;
       int   dig3:7;
    }bits;
    struct
    {
       int   icons;
       int   lcd1;
       int   lcd2;
       int   lcd3;
    }bytes;
}

union LCD_BUF disp1, disp2, disp3;


I need few function to operation of displays
f.e
void InsertInteger(...)
void SetPoint(...)
etc.

How should I use reference of union?
Ttelmah
Guest







PostPosted: Wed Oct 03, 2007 5:14 am     Reply with quote

Exactly the same way as with any other variable. So:

void InsertInteger(union LCD_BUF val) {

}

Then call this with (for your first variable shown):

InsertInteger(disp1);

While inside the routine, use (for example)

val.icons

Best Wishes
Andrew-miksys



Joined: 11 Sep 2007
Posts: 7

View user's profile Send private message

PostPosted: Wed Oct 03, 2007 6:32 am     Reply with quote

Thanks,
I'm not sure but this way will be created local copy of union wich be exist only inside function body. So any operation has no effect after end of function. Am I right?
Ttelmah
Guest







PostPosted: Wed Oct 03, 2007 7:00 am     Reply with quote

Yes.
If you want to reference the external union, thre are two routes:
1) Declare the variables as global (declare them outside the body of the main function). Then you can refer to these anywhere.
2) Pass a pointer to the external declaration. For this:

void InsertInteger(union LCD_BUF * val) {

}
Note the '*', which says thatthis value is a 'pointer to' a value of this type.
Call this with:

InsertInteger(&disp1);

Note the '&', which means pass the address of this.
Then in the function, use:

val->icons

Note that unions, and structures are identical in their syntax here, both allowing the -> shortcut to access a member,rather than having to use the * to 'dereference' the pointer.
A union, is actually a type of structure in C, which has two (or more) different sets of names, referencing the same data.

Best Wishes
Andrew-miksys



Joined: 11 Sep 2007
Posts: 7

View user's profile Send private message

PostPosted: Wed Oct 03, 2007 9:03 am     Reply with quote

Thanks!
I tested it berofe but forgot of -> operator Embarassed
It works of course.
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