D
Dan Pop
In said:Christian Bau said:It is a very obscure thing, and difficult to write code that actually
creates the situation. Something like this:
typedef struct { int x; int a [2]; } mystruct;
static mystruct r, s;
static int i;
(i > 0 ? r : s).a [1] = 0;
The result of the ?: is some object, but not one of the objects r, s: It
is an unnamed object which contains either a copy of r or a copy of s.
Modifying that unnamed object invokes undefined behavior. You may have
to do some more work to make this compile.
I guess the "proper" way to achieve this would be :
(i > 0 ? &r : &s)->a[1] = 0;
Are we dealing with the same issue in the code :
(ignore_case ? strcasecmp : strcmp) (s1, s2);
assuming proper declaration of strcasecmp.
Nope. You're not attempting to modify the result of the conditional
operator.
How should it be written then ?
Apart from stylistic issues, your version is correct. In real code,
you may want to either use a function pointer (if ignore_case doesn't
change its value in "random" places during normal program execution)
or to hide the conditional operator behind a macro (if it does).
Dan