Language gurus: Is this a valid structure?

1
2
3
4
5
6
7
8
typedef union
  {
  uint16_t gray;
  uint16_t rgb[ 3 ];
  struct { uint16_t red, green, blue; }
  uint8_t  alpha;
  }
  tRNS_t;

I ask just because in my code it is more convenient to refer to the RGB components by name, and this is prettier than something like rgb.red, etc.

Nothing critical, of course, but I am wondering if anyone's C89 compiler will choke on it.
I've always used:

struct {
unsigned char Red, Green, Blue;
} Colour;

RGB only go up to 255 so you don't need anymore than 1 byte per one.

Edit: MingW 3.4.5 won't compile your code. Because your internal struct doesn't have a name.
Last edited on
Thanks for the response. That is basically what I was wondering. I only tested it with MinGW 4.3.0 alpha (which compiles it fine), and all the docs I can remember having seen (which are few on unions) only say that unnamed members are not initialized.


This is for a PNG handler. The tRNS chunk uses a fixed 16-bits space per sample storage for GrayScale and RGB images (the decoder has to ignore high-order bits outside the image's actual bit-depth).

I've actually been working with Tcl lately. The decoder uses the Critcl package to attempt to compile a native (C) library before using the interpreted version. The difference in execution speed is significant for images greater than 28 pixels square. And while I prefer the simpler syntax, it is more important that the C code be as robustly portable as possible...

Alas.

Thanks!
Last edited on
Topic archived. No new replies allowed.