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. 9 1752
perry.yuan wrote:
#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.
you can do two things. one is to create a "write" macro:
#define SET_ELEMENT(v_struct, idx, value) \
if (..) { v_struct.x = value; } else { v_struct.y = value; }
or you can use a function to return the right address, and have the
macro deref it for you.
inline int *get_vstruct_addr(struct vstruct *p, int idx) { return
&p->field; }
#define ELEMENT_OF_VARRAY(vstruct, idx) (*get_vstruct_addr(&vstruct,
idx))
>
you can do two things. one is to create a "write" macro:
#define SET_ELEMENT(v_struct, idx, value) \
if (..) { v_struct.x = value; } else { v_struct.y = value; }
or you can use a function to return the right address, and have the
macro deref it for you.
inline int *get_vstruct_addr(struct vstruct *p, int idx) { return
&p->field; }
#define ELEMENT_OF_VARRAY(vstruct, idx) (*get_vstruct_addr(&vstruct,
idx))
Thanks for your reply.
But I need an expression (could be embedded in any statement as user
likes) instead of a statement.
get_vstruct_addr() doesn't work because of its returning an address of
bit field.
perry.yuan wrote:
>
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.
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.
(*(condition ? pointer_1 : pointer_2))
--
pete
perry.yuan wrote:
you can do two things. one is to create a "write" macro:
#define SET_ELEMENT(v_struct, idx, value) \
if (..) { v_struct.x = value; } else { v_struct.y = value; }
or you can use a function to return the right address, and have the
macro deref it for you.
inline int *get_vstruct_addr(struct vstruct *p, int idx) { return
&p->field; }
#define ELEMENT_OF_VARRAY(vstruct, idx) (*get_vstruct_addr(&vstruct,
idx))
Thanks for your reply.
But I need an expression (could be embedded in any statement as user
likes) instead of a statement.
get_vstruct_addr() doesn't work because of its returning an address of
bit field.
You can rewrite the first suggestion as a single expression:
#define SET_ELEMENT(v_struct, idx, value) \
((...) ? ((v_struct).x = (value)) : ((v_struct).y = (value)))
Some of the parentheses may be unnecessary, but I haven't checked,
since they don't hurt. If you're working with bit-fields, I think
that's the closest you'll get to what you want.
"perry.yuan" <pe********@gmail.comwrote:
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.
1. Don't Do That, it's obfuscatory;
2. Read the bleedin' FAQ: <http://c-faq.com/expr/qcolonlhs.html>.
Richard
Richard Bos wrote:
"perry.yuan" <pe********@gmail.comwrote:
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.
1. Don't Do That, it's obfuscatory;
2. Read the bleedin' FAQ: <http://c-faq.com/expr/qcolonlhs.html>.
Richard
Thanks, Richard and all others.
My working environment is in a rather old C language programming style
so that a macro seems to be the best fit. I'd like to write in macro an
expression that could be embedded in other expressions as either rvalue
or lvalue.
The mean
*((condition) ? &a : &b) = complicated_expression;
is a good idea to me. However, in my case, it still doesn't work: I
could not take address of bit field. It seems I have to design a
function instead of a expression.
Thanks again.
perry.yuan wrote:
I don't think you can ever use a bitfield in a conditional lvalue. You
see it's a very tricky thing to do at run-time-- the compiler would
have to conditionally jump to different bits of code depending on what
was being stored into. This is theoretically doable, but it clashes
with the way most optimizers want the parse tree to look like.
perry.yuan wrote:
inline int *get_vstruct_addr(struct vstruct *p, int idx) { return
&p->field; }
#define ELEMENT_OF_VARRAY(vstruct, idx) (*get_vstruct_addr(&vstruct,
idx))
get_vstruct_addr() doesn't work because of its returning an address of
bit field.
does it need to be a bitfield? if your compiler has 8bit chars, can't
you just use char?
tedu wrote:
perry.yuan wrote:
>
inline int *get_vstruct_addr(struct vstruct *p, int idx) { return
&p->field; }
#define ELEMENT_OF_VARRAY(vstruct, idx) (*get_vstruct_addr(&vstruct,
idx))
get_vstruct_addr() doesn't work because of its returning an address of
bit field.
does it need to be a bitfield? if your compiler has 8bit chars, can't
you just use char?
I do it by this
0[cond?ptr1:ptr2 ]=expr; This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: soup_or_power |
last post by:
Sorry these are not c++ questions per se. Your help is appreciated.
Suppose arr is an array (e.g. int arr) Here are some questions:
a)What does arr point to?
My answer: to the first element of...
|
by: Romeo Colacitti |
last post by:
Hi,
Does anyone here have a strong understanding for the meanings of the
terms "lvalue" and "rvalue" as it pertains to C, objects, and different
contexts? If so please share.
I've been...
|
by: priya |
last post by:
Hi all,
I am new to this group.I am working in c language.I have dount
in pointer? how to retun array of pointer in function?
example
main()
|
by: Kavya |
last post by:
Can someone give and explain in simple terms a definition of lvalue?
Also what are these modifiable and non-modifiable lvalues?
I always thought that, if we can assign to anything then that...
|
by: the_init |
last post by:
Hi friends,
I read about Lvalue in previous posting and Googled it but I'm not
understood it completely.
There is a small doubt.
int a;
a=20; // here a is Lvalue
But
|
by: nobrow |
last post by:
Yes I know what lvalue means, but what I want to ask you guys about is
what are all valid lvalues ...
a
*a
a
*(a + 1)
....
What else?
|
by: Jess |
last post by:
Hello,
Sometimes declarations are all what we need when we define/declare
classes (or functions?), but sometimes we need definitions. I learned
that if we define a class (B) that has an object...
|
by: Pietro Cerutti |
last post by:
Hi group,
assume the following declarations:
char *func_1(void);
void func_2(char **);
I am allowed to do:
char *c = func_1();
|
by: Davy |
last post by:
Hi all,
I am writing a function, which return the pointer of the int. But it
seems to be wrong. Any suggestion?
int * get_p_t(int t) {
return &t;
}
int main()
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |