P
perry.yuan
Hi there,
I got a problem: how to return an lvalue from conditional expression in
C programming language. Any solution or suggestion to my problem in
either C or C++ is welcome.
On the CPU (actually it is a DSP), memory is addressed in 16 bit unit
(called word).
There is a struct of variable size (in pseudo code):
typedef struct V_STRUCT {
unsigned length : 8; /* 8 bit field. Value range [1..99] */
unsigned_8_bit_field varray[length];
} v_struct_t;
e.g. if the value of the field length is 4, the struct will look like
(in pseudo code):
typedef struct V_STRUCT {
unsigned length : 8; /* Value 4. MSB of Word #1. */
unsigned varray[0]: 8; /* LSB of Word #1. */
unsigned varray[1]: 8; /* MSB of Word #2. */
unsigned varray[2]: 8; /* LSB of Word #2. */
unsigned varray[3]: 8; /* MSB of Word #3. */
unsigned alignment_filling: 8; /* LSB of Word #3. */
} v_struct_t;
The real definition of the struct is:
typedef struct V_STRUCT {
unsigned length : 8;
unsigned varray : 8;
} v_struct_t;
I would like to provide a convenient macro to access element of the
varray:
typedef struct DSPWORD {
unsigned msb : 8;
unsigned lsb : 8;
} dspword_t;
#define ELEMENT_OF_VARRAY( v_struct, idx_of_varray ) \
( ( (idx_of_varray) & 0x0001 ) \
? \
( ( ( (dspword_t *) &(v_struct) ) + ( ( (idx_of_varray) + 1) >> 1
) )->msb ) \
: \
( ( ( (dspword_t *) &(v_struct) ) + ( ( (idx_of_varray) + 1) >> 1
) )->lsb ) \
)
The problem is that, according to the C99 Standard, conditional
expression could not return an lvalue so that I can only use the macro
ELEMENT_OF_VARRAY as rvalue but not as lvalue. However, I'd like use it
as both rvalue and lvalue.
Any solution/trick/suggestion to that? TIA.
I got a problem: how to return an lvalue from conditional expression in
C programming language. Any solution or suggestion to my problem in
either C or C++ is welcome.
On the CPU (actually it is a DSP), memory is addressed in 16 bit unit
(called word).
There is a struct of variable size (in pseudo code):
typedef struct V_STRUCT {
unsigned length : 8; /* 8 bit field. Value range [1..99] */
unsigned_8_bit_field varray[length];
} v_struct_t;
e.g. if the value of the field length is 4, the struct will look like
(in pseudo code):
typedef struct V_STRUCT {
unsigned length : 8; /* Value 4. MSB of Word #1. */
unsigned varray[0]: 8; /* LSB of Word #1. */
unsigned varray[1]: 8; /* MSB of Word #2. */
unsigned varray[2]: 8; /* LSB of Word #2. */
unsigned varray[3]: 8; /* MSB of Word #3. */
unsigned alignment_filling: 8; /* LSB of Word #3. */
} v_struct_t;
The real definition of the struct is:
typedef struct V_STRUCT {
unsigned length : 8;
unsigned varray : 8;
} v_struct_t;
I would like to provide a convenient macro to access element of the
varray:
typedef struct DSPWORD {
unsigned msb : 8;
unsigned lsb : 8;
} dspword_t;
#define ELEMENT_OF_VARRAY( v_struct, idx_of_varray ) \
( ( (idx_of_varray) & 0x0001 ) \
? \
( ( ( (dspword_t *) &(v_struct) ) + ( ( (idx_of_varray) + 1) >> 1
) )->msb ) \
: \
( ( ( (dspword_t *) &(v_struct) ) + ( ( (idx_of_varray) + 1) >> 1
) )->lsb ) \
)
The problem is that, according to the C99 Standard, conditional
expression could not return an lvalue so that I can only use the macro
ELEMENT_OF_VARRAY as rvalue but not as lvalue. However, I'd like use it
as both rvalue and lvalue.
Any solution/trick/suggestion to that? TIA.