#include <stdio.h>
struct object {
int inArray;
union ID {
int index;
void * address;
} id;
};
typedef int (*f_init)(struct object *obj, int inx, void * addr);
typedef void (*f_report)(const struct object *obj);
int object_init(struct object *obj, int inx, void * addr)
{
if (obj) {
if (obj->inArray)
obj->id.index = inx;
else
obj->id.address = addr;
return 1;
}
else
return 0;
}
void object_report(const struct object *obj)
{
if (obj) {
if ((*obj).inArray)
printf("object index: %d.\n", (*obj).id.index);
else
printf("object address: %p.\n", (*obj).id.address);
}
}
int main(int argc, char *argv[])
{
struct object single_obj, obj_arr[2];
f_init init = object_init;
f_report report = object_report;
single_obj.inArray = 0;
(void)(*init)(&single_obj, -1, &single_obj);
report(&single_obj);
obj_arr[0] = single_obj;
0[obj_arr].inArray = 1;
(void)(*init)(obj_arr, 0, NULL);
report(obj_arr);
return 0;
}