How to understand the union part in this C segment

F

fl

Hi,

I am programming my routine with a commerical software product. I want
to make the software standalone. Thus, I want to understand the
interface definition, see below in the end.

"number_array" is the union? Then, I can see only one constitute of
this union, a struct. According to the union definition, there should
have several options for a union. What is the problem? Because this C
part is from Matlab matrix.h file, I do not think it is an error.


My second question is about the "} data" line. What is data for? It is
the name of union?


Thanks a lot, please explain it to me if you could.








union {
struct {
void *pdata;
void *pimag_data;
void *reserved5;
int reserved6[3];
} number_array;
} data;



...............................
struct mxArray_tag0 {
void *reserved;
int reserved1[2];
void *reserved2;
int number_of_dims;
unsigned int reserved3;
struct {
unsigned int scalar_flag : 1;
unsigned int flag1 : 1;
unsigned int flag2 : 1;
unsigned int flag3 : 1;
unsigned int flag4 : 1;
unsigned int flag5 : 1;
unsigned int flag6 : 1;
unsigned int flag7 : 1;
unsigned int private_data_flag : 1;
unsigned int flag8 : 1;
unsigned int flag9 : 1;
unsigned int flag10 : 1;
unsigned int flag11 : 4;
unsigned int flag12 : 8;
unsigned int flag13 : 8;
} flags;
unsigned int reserved4[2];


union {
struct {
void *pdata;
void *pimag_data;
void *reserved5;
int reserved6[3];
} number_array;
} data;
};
 
K

Keith Thompson

fl said:
I am programming my routine with a commerical software product. I want
to make the software standalone. Thus, I want to understand the
interface definition, see below in the end.

"number_array" is the union? Then, I can see only one constitute of
this union, a struct. According to the union definition, there should
have several options for a union. What is the problem? Because this C
part is from Matlab matrix.h file, I do not think it is an error.

My second question is about the "} data" line. What is data for? It is
the name of union?

union {
struct {
void *pdata;
void *pimag_data;
void *reserved5;
int reserved6[3];
} number_array;
} data;

This declares an anonymous union type, and a single object of that type
named "data".

The union type has exatly one member. That member's name is
"number_array", and its type is an anonymous struct type;
the struct type has 4 members.

I don't know why the union has only one member, but a Google search
turned up a copy of the matrix.h file. You should probably pay
attention to the commend associated with the declaration:

/*
* This modified version of the mxArray structure is needed to support
* the ARRAY_ACCESS_INLINING macros. NOTE: The elements in this structure
* should not be accessed directly. Inlined MEX-files are NOT guaranteed
* to be portable from one release of MATLAB to another.
*/

My guess is that it's intended to be compatible with some other
type where the corresponding union has two or more members, but
that anything you do that depends on the internals of the type is
likely to get you into trouble.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,954
Messages
2,570,114
Members
46,702
Latest member
VernitaGow

Latest Threads

Top