View previous topic :: View next topic |
Author |
Message |
Andrew-miksys
Joined: 11 Sep 2007 Posts: 7
|
Calling with a reference of union |
Posted: Wed Oct 03, 2007 3:08 am |
|
|
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? |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah Guest
|
|
Posted: Wed Oct 03, 2007 5:14 am |
|
|
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 |
|
![](templates/subSilver/images/spacer.gif) |
Andrew-miksys
Joined: 11 Sep 2007 Posts: 7
|
|
Posted: Wed Oct 03, 2007 6:32 am |
|
|
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? |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah Guest
|
|
Posted: Wed Oct 03, 2007 7:00 am |
|
|
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 |
|
![](templates/subSilver/images/spacer.gif) |
Andrew-miksys
Joined: 11 Sep 2007 Posts: 7
|
|
Posted: Wed Oct 03, 2007 9:03 am |
|
|
Thanks!
I tested it berofe but forgot of -> operator
It works of course. |
|
![](templates/subSilver/images/spacer.gif) |
|