C Union new instance

The Union object allows you to define a C union. To create a new C union instance from this definition, simply call this object with or without initialization values.

Notes

To initialize Union fields, you can provide as arguments :
  • A table that contains the fields with the corresponding values.
  • Any Value, Struct, Union or [ ] Array.
  • If no initialization value is provided, all union fields are set to 0 or NULL

Example

local c = require "c" -- Defines a RGBA Struct C definition local RGBA = c.Struct("CCCC", "red", "green", "blue", "alpha") -- Creates a COLOR Union C definition local COLOR = c.Union(".I", {rgba = RGBA}, "value") -- Creates a new COLOR instance with arguments initalization local red = COLOR { value = 0xFF0000FF } print(string.format("Color.value is 0x%X", red.value)) print(string.format("\t|- Red:\t\t0x%.2X", red.rgba.red)) print(string.format("\t|- Green:\t0x%.2X", red.rgba.green)) print(string.format("\t|- Blue:\t0x%.2X", red.rgba.blue)) print(string.format("\t|- Alpha:\t0x%.2X", red.rgba.alpha))