Unions and portability...

P

prashna

Hi all,
Consider a function which gives huge number of datum in the form of
array. I know how the data is arranged in the array (i.e. what index
stores what data).

void foo( int32 *data);

The parameter I am passing to this function is an array declared in
union and the other element in the union is the structure which
contains the list of all the data.

typedef struct
{
int32_t i1; /*Index 0*/
real32_t r1; /*Index 1*/
char string[4]; /*Index 2*/
.......
.......
}ns_data_list_t;/*All the elemnts are 32 bit*/

typedef union
{
real32_t name_strg_data[MAX_NS_DATA];
ns_data_list_t ns_data_list;
} ns_data_t;

I am passing ns_data.name_strg_data as the argument in which the data
is stored and I am accessing the data elements using the structure
ns_data_list_t.
Kindly let me know if this is not correct(At least on Linux). Is there
any other reliable, portable method of doing the same? Note that I
cannot change the prototype of the function foo.
I am using gcc on linux...
 
C

Christian Bau

Hi all,
Consider a function which gives huge number of datum in the form of
array. I know how the data is arranged in the array (i.e. what index
stores what data).

void foo( int32 *data);

The parameter I am passing to this function is an array declared in
union and the other element in the union is the structure which
contains the list of all the data.

typedef struct
{
int32_t i1; /*Index 0*/
real32_t r1; /*Index 1*/
char string[4]; /*Index 2*/
.......
.......
}ns_data_list_t;/*All the elemnts are 32 bit*/

typedef union
{
real32_t name_strg_data[MAX_NS_DATA];
ns_data_list_t ns_data_list;
} ns_data_t;

I am passing ns_data.name_strg_data as the argument in which the data
is stored and I am accessing the data elements using the structure
ns_data_list_t.
Kindly let me know if this is not correct(At least on Linux). Is there
any other reliable, portable method of doing the same? Note that I
cannot change the prototype of the function foo.

You say you can't change the prototype of function foo. Maybe you can
shoot the person who "designed" this and get a bit of satisfaction that
way?
 
J

Jack Klein

Hi all,
Consider a function which gives huge number of datum in the form of
array. I know how the data is arranged in the array (i.e. what index
stores what data).

void foo( int32 *data);

The parameter I am passing to this function is an array declared in
union and the other element in the union is the structure which
contains the list of all the data.

typedef struct
{
int32_t i1; /*Index 0*/
real32_t r1; /*Index 1*/
char string[4]; /*Index 2*/
.......
.......
}ns_data_list_t;/*All the elemnts are 32 bit*/

typedef union
{
real32_t name_strg_data[MAX_NS_DATA];
ns_data_list_t ns_data_list;
} ns_data_t;

I am passing ns_data.name_strg_data as the argument in which the data
is stored and I am accessing the data elements using the structure
ns_data_list_t.
Kindly let me know if this is not correct(At least on Linux). Is there
any other reliable, portable method of doing the same? Note that I
cannot change the prototype of the function foo.
I am using gcc on linux...

No, it is not guaranteed to be portable or correct, not by the C
standard. Whether it is portable or not on Linux/gcc is a matter for

The proper method is to use memcpy() on each element of the raw data
(array) to copy it to the appropriate member of a properly defined
structure.
 

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

Similar Threads


Members online

Forum statistics

Threads
474,150
Messages
2,570,853
Members
47,394
Latest member
Olekdev

Latest Threads

Top