What I was trying to say, but in better form! On 01/16/2013 08:11 PM, Byron Jeff wrote: > On Wed, Jan 16, 2013 at 04:01:08PM -0800, Bob Blick wrote: >> Is this a correct way to declare and call a function that uses a union >> as the argument, and pass it a value that is not declared as union? > No. They are different types, so it cannot be done. > > I always think of a union as a structure whose variables share the same > memory. So the bottom line is that it's a structure. So you cannot impres= s > a simple type on top of it. > > The simplest way to resolve the problem is to convert the parameters to > pointers. Even though pointers are type specific, they can be readily > interchanged. > > Finally I would go ahead and typedef the union (and its pointer) so that > you can specify the type by name. > >> //declaring the function >> void myDumbFunction(union {unsigned int uint; signed int ssint; unsigned >> char[2] unchar}); >> >> // calling it from somewhere in my program >> unsigned int dumbvar; >> dumbvar =3D somedumbvalue; >> myDumbFunction(dumbvar); >> >> // the function >> void myDumbFunction(somevar){ >> if (somevar.unchar[1]) >> do_something(); >> } >> >> Is that going to work, the compiler should know that somevar is an >> unsigned int(not that it should matter since it's just memory space that >> is the size of the union), so within my function I can access >> somevar.uint somevar.ssint as well as somevar.unchar[0] and >> somevar.unchar[1] > Again the problem is that the parameter is typed, and the types do not > match. I would not feel confortable trying to cast the simple types to th= e > union. Pointers are much more predictable in their behavior. > >> That all seem OK? I know that in most cases using a union serves no real >> purpose other than to be dangerous, there are other ways to do things. >> But my question is whether this is accurate or how it would be made >> correct if it isn't. >> > Here's my crack at it given my explanation above: > > // Typedef the union and its pointer > typedef union {unsigned int uint; signed int ssint; unsigned char[2] unch= ar} my_u, *my_p; > > //declaring the function > void myDumbFunction(void *somevar); // The void facilitates passing any t= ype of pointer in. > > // calling it from somewhere in my program > unsigned int dumbvar; > dumbvar =3D somedumbvalue; > myDumbFunction(&dumbvar); // Pass in the unsigned int pointer to the u= nion pointer of the function > > // the function > void myDumbFunction(void *somevar){ > my_p u1 =3D somevar; // Convert pointer into a union pointer > > if (u1->unchar[1]) > do_something(); > } > >> Thanks, > Happy to help. > > BAJ > >> Bob >> >> --=20 >> http://www.fastmail.fm - Accessible with your email software >> or over the web >> >> --=20 >> http://www.piclist.com PIC/SX FAQ & list archive >> View/change your membership options at >> http://mailman.mit.edu/mailman/listinfo/piclist --=20 Joe Wronski Stillwater Embedded Engineering www.stillwatereng.net --=20 http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist .