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 CCS Technical Support

Pointer to union

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



Joined: 17 Sep 2004
Posts: 133
Location: UK

View user's profile Send private message

Pointer to union
PostPosted: Thu Apr 21, 2005 6:30 am     Reply with quote

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

View user's profile Send private message Send e-mail

PostPosted: Thu Apr 21, 2005 7:02 am     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Apr 21, 2005 7:06 am     Reply with quote

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

View user's profile Send private message Send e-mail

PostPosted: Thu Apr 21, 2005 7:11 am     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Apr 21, 2005 7:16 am     Reply with quote

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

View user's profile Send private message Send e-mail

PostPosted: Thu Apr 21, 2005 7:16 am     Reply with quote

and how is the other function declared?
alexz



Joined: 17 Sep 2004
Posts: 133
Location: UK

View user's profile Send private message

PostPosted: Thu Apr 21, 2005 7:19 am     Reply with quote

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

View user's profile Send private message Send e-mail

PostPosted: Thu Apr 21, 2005 7:22 am     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Apr 21, 2005 7:24 am     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Apr 21, 2005 7:30 am     Reply with quote

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

View user's profile Send private message Send e-mail

PostPosted: Thu Apr 21, 2005 7:34 am     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Apr 21, 2005 7:44 am     Reply with quote

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
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