Creating sequence points using macros

A

ais523

The program excerpt

int i;
char c;
char* a[]= {"abc","def","ghi"};
/* ... */
i=0;
c=a[i++][i++];

obviously invokes undefined behaviour, because i is modified twice
between sequence points. I was wondering if it would be possible to
define some sort of macro to generate a sequence point, so that
c=seq(a[i++])[i++];
could be written and lead to the behaviour being fully defined. (The
idea is that a sequence point would be introduced after the i++ on the
left was evaluated, forcing i to be incremented before the second i++
even runs and avoiding the UB.) Apart from splitting the statement
into two statements (which would probably be the best way to do this
in practice), is there any portable C89 or C99 way to define seq? (By
the way, if it didn't have to work with multiple data types, would a
function that returns its own argument work?)
 
C

Chris Dollin

ais523 said:
The program excerpt

int i;
char c;
char* a[]= {"abc","def","ghi"};
/* ... */
i=0;
c=a[i++][i++];

obviously invokes undefined behaviour, because i is modified twice
between sequence points. I was wondering if it would be possible to
define some sort of macro to generate a sequence point, so that
c=seq(a[i++])[i++];
could be written and lead to the behaviour being fully defined. (The
idea is that a sequence point would be introduced after the i++ on the
left was evaluated, forcing i to be incremented before the second i++
even runs and avoiding the UB.) Apart from splitting the statement
into two statements (which would probably be the best way to do this
in practice),

Or
c = a[i+1], i += 2;

One statement.
is there any portable C89 or C99 way to define seq?

As a macro? I don't think so.
(By
the way, if it didn't have to work with multiple data types, would a
function that returns its own argument work?)

(fx:trance)

I don't think so. If `seq` is a function, in `seq( a[i++] )[i++]`
there's no reason that the second `i++` has to be evaluated after
the function call (ie after the sequence point), so it's just as
undefined as if the sequence point wasn't there.

But I'm not a sequence-point expert.

--
The second Jena user conference! http://hpl.hp.com/conferences/juc2007/
Nit-picking is best done among friends.

Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN
 
A

ais523

ais523 wrote: (snip)
I was wondering if it would be possible to
define some sort of macro to generate a sequence point, so that
c=seq(a[i++])[i++];
could be written and lead to the behaviour being fully defined. (snip)
(By
the way, if it didn't have to work with multiple data types, would a
function that returns its own argument work?)

(fx:trance)

I don't think so. If `seq` is a function, in `seq( a[i++] )[i++]`
there's no reason that the second `i++` has to be evaluated after
the function call (ie after the sequence point), so it's just as
undefined as if the sequence point wasn't there.

But I'm not a sequence-point expert.
That's pretty much what I'd thought, but I wanted to make sure anyway.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top