View previous topic :: View next topic |
Author |
Message |
alexz
Joined: 17 Sep 2004 Posts: 133 Location: UK
|
Pointer to union |
Posted: Thu Apr 21, 2005 6:30 am |
|
|
I have got the following union:
extern union unionFloat
{
float fl;
unsigned char cha[4];
};
How to make a pointer to it and how to use it?
Thank you _________________ Alex |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Thu Apr 21, 2005 7:02 am |
|
|
Code: |
typedef union
{
float fl;
unsigned char cha[4];
}UNION_FLOAT;
extern UNION_FLOAT unionFloat;
extern UNION_FLOAT * ptr_unionFloat;
|
|
|
|
alexz
Joined: 17 Sep 2004 Posts: 133 Location: UK
|
|
Posted: Thu Apr 21, 2005 7:06 am |
|
|
Right,
and how to use it?
I mean, I want to point to the UNION_FLOAT.fl _________________ Alex |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Thu Apr 21, 2005 7:11 am |
|
|
Code: |
ptr_unionFloat->float = 54.5;
ptr_unionFloat->cha[0] = 1;
ptr_unionFloat->cha[1] = 2;
ptr_unionFloat->cha[2] = 3;
ptr_unionFloat->cha[3] = 4;
|
|
|
|
alexz
Joined: 17 Sep 2004 Posts: 133 Location: UK
|
|
Posted: Thu Apr 21, 2005 7:16 am |
|
|
I pass the pointer to another function like so: ptr_uFloat->fl
I get the error: Error [1146] type mismatch in argument 2
I need to pass the address of the value in the uFloat.fl _________________ Alex
Last edited by alexz on Thu Apr 21, 2005 7:18 am; edited 1 time in total |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Thu Apr 21, 2005 7:16 am |
|
|
and how is the other function declared? |
|
|
alexz
Joined: 17 Sep 2004 Posts: 133 Location: UK
|
|
Posted: Thu Apr 21, 2005 7:19 am |
|
|
Mark wrote: | and how is the other function declared? |
extern void writeSFlash(uchar noBytes, uchar *sfData); _________________ Alex |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Thu Apr 21, 2005 7:22 am |
|
|
Code: |
writeSFlash(sizeof(unionFloat.cha),ptr_unionFloat->cha);
|
Last edited by Mark on Thu Apr 21, 2005 7:31 am; edited 1 time in total |
|
|
alexz
Joined: 17 Sep 2004 Posts: 133 Location: UK
|
|
Posted: Thu Apr 21, 2005 7:24 am |
|
|
Mark wrote: | Code: |
writeSFlash(sizeof(ptr_unionFloat->cha),ptr_unionFloat->cha);
|
|
Mark, I can't change it, because this function is called a lot and addresses of variables and arrays are passed to it. _________________ Alex |
|
|
alexz
Joined: 17 Sep 2004 Posts: 133 Location: UK
|
|
Posted: Thu Apr 21, 2005 7:30 am |
|
|
Thank you for the help Mark.
I think I have found the way to do it
writeSFlash(0x04,(unsigned char *)ptr_uFloat); _________________ Alex |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Thu Apr 21, 2005 7:34 am |
|
|
You don't need a pointer to the union if you are just trying to pass and address
Code: |
writeSFlash(4,unionFloat.cha);
writeSFlash(4,&unionFloat.cha[0]);
writeSFlash(4,(uchar*)&unionFloat.fl);
|
Either of the above would work for you. |
|
|
alexz
Joined: 17 Sep 2004 Posts: 133 Location: UK
|
|
Posted: Thu Apr 21, 2005 7:44 am |
|
|
Mark wrote: | You don't need a pointer to the union if you are just trying to pass and address
Code: |
writeSFlash(4,unionFloat.cha);
writeSFlash(4,&unionFloat.cha[0]);
writeSFlash(4,(uchar*)&unionFloat.fl);
|
Either of the above would work for you. |
Thanks Mark, yes, it is working _________________ Alex |
|
|
|