Increment operator question

  • Thread starter Mike Pennington
  • Start date
M

Mike Pennington

Recently we came upon the following construct in some code from a system
that we were rehosting:

index = index++;

The intent of the programmer was to increment the index by 1. On the
old system this worked as desired.
On the new system, with a different compiler, the value of index was
never changed. I know that the way
that it should have been coded was either

index++;

or

index += 1;

My question is whether anyone knows if there is a defined interpretation
of the statement as originally
written and if so what is it?

Thanks
 
A

Artie Gold

Mike said:
Recently we came upon the following construct in some code from a system
that we were rehosting:

index = index++;

The intent of the programmer was to increment the index by 1. On the
old system this worked as desired.
On the new system, with a different compiler, the value of index was
never changed. I know that the way
that it should have been coded was either

index++;

or

index += 1;

My question is whether anyone knows if there is a defined interpretation
of the statement as originally
written and if so what is it?

It's undefined behavior as `index' is modified multiple times without an
intervening sequence point.

See: http://www.eskimo.com/~scs/C-faq/s3.html
(the rules for C and C++ are the same in this respect).

HTH,
--ag
 

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,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top