mardi 28 janvier 2014

Syntax for union parameter topic




I have had occasion where I've needed to do something like this:

struct SBGRA
{
unsigned char blu;
unsigned char grn;
unsigned char red;
unsigned char alp;
};

void main(void)
{
struct SBGRA color = { 255,255,255,255 }; // white, full alpha
foo(color); // do something with that color
}

void foo(SBGRA color)
{
union {
struct SBGRA color;
unsigned int _color;
} u;

// Convert SBGRA to an unsigned int
u.color = color;
some_other_function(u._color); // Do something with that number
}

-----
Is there a way to do something like this in C (to declare a union in
the function definition line)?

void foo(union { SBGRA color, unsigned int _color } u)
{
some_other_function(u._color);
}

There are only certain occasions where I need it, and rather than declare
something global, it would be nice to be able to declare the local union
in the definition to allow existing data passed in the structure format
to continue unchanged, but then to access it without the extra local
union definition, and in non-structure form.

Best regards,
Rick C. Hodgin





Aucun commentaire:

Enregistrer un commentaire