From: Chris Saunders on
Sorry for asking this - I haven't been using C for a long time and I'm
adapting some C code to another language. Here is a line from the C code
I'm adaptinge:

x = mt[mti++];

Is x set to mt[mti] or mt[mti + 1]. after this statement is executed.

Regards
Chris Saunders

From: Scott McPhillips [MVP] on
"Chris Saunders" <evas(a)mountaincable.net> wrote in message
news:30E53076-8F23-49DE-A1A8-D45FA1D1ACD5(a)microsoft.com...
> Sorry for asking this - I haven't been using C for a long time and I'm
> adapting some C code to another language. Here is a line from the C code
> I'm adaptinge:
>
> x = mt[mti++];
>
> Is x set to mt[mti] or mt[mti + 1]. after this statement is executed.
>
> Regards
> Chris Saunders

x = mt[mti];

Then the ++ is performed.

--
Scott McPhillips [VC++ MVP]

From: Chris Saunders on
Thanks for the reply Scott. Unfortunatly your answer was not clear to me.
Perhaps my question wasn't clear enough. I'll try to be clearer. Is the ++
performed before or after x is set? I don't think x = mt[mti++] is the same
as x = mt[++mti] is it?

Regards
Chris Saunders

"Scott McPhillips [MVP]" <org-dot-mvps-at-scottmcp> wrote in message
news:%23E%23MsUMmKHA.1652(a)TK2MSFTNGP05.phx.gbl...
> "Chris Saunders" <evas(a)mountaincable.net> wrote in message
> news:30E53076-8F23-49DE-A1A8-D45FA1D1ACD5(a)microsoft.com...
>> Sorry for asking this - I haven't been using C for a long time and I'm
>> adapting some C code to another language. Here is a line from the C code
>> I'm adaptinge:
>>
>> x = mt[mti++];
>>
>> Is x set to mt[mti] or mt[mti + 1]. after this statement is executed.
>>
>> Regards
>> Chris Saunders
>
> x = mt[mti];
>
> Then the ++ is performed.
>
> --
> Scott McPhillips [VC++ MVP]

From: Chris Saunders on
I re-read your reply Scott and got it this time. Thanks again.

Regards
Chris Saunders

"Chris Saunders" <evas(a)mountaincable.net> wrote in message
news:%23TY9kjMmKHA.5020(a)TK2MSFTNGP02.phx.gbl...
> Thanks for the reply Scott. Unfortunatly your answer was not clear to me.
> Perhaps my question wasn't clear enough. I'll try to be clearer. Is the
> ++ performed before or after x is set? I don't think x = mt[mti++] is the
> same as x = mt[++mti] is it?
>
> Regards
> Chris Saunders
>
> "Scott McPhillips [MVP]" <org-dot-mvps-at-scottmcp> wrote in message
> news:%23E%23MsUMmKHA.1652(a)TK2MSFTNGP05.phx.gbl...
>> "Chris Saunders" <evas(a)mountaincable.net> wrote in message
>> news:30E53076-8F23-49DE-A1A8-D45FA1D1ACD5(a)microsoft.com...
>>> Sorry for asking this - I haven't been using C for a long time and I'm
>>> adapting some C code to another language. Here is a line from the C
>>> code I'm adaptinge:
>>>
>>> x = mt[mti++];
>>>
>>> Is x set to mt[mti] or mt[mti + 1]. after this statement is executed.
>>>
>>> Regards
>>> Chris Saunders
>>
>> x = mt[mti];
>>
>> Then the ++ is performed.
>>
>> --
>> Scott McPhillips [VC++ MVP]
>

From: Ulrich Eckhardt on
"Scott McPhillips [MVP]" <org-dot-mvps-at-scottmcp> wrote:
> "Chris Saunders" <evas(a)mountaincable.net> wrote [...]
>>
>> x = mt[mti++];
>>
>> Is x set to mt[mti] or mt[mti + 1]. after this statement is executed.
>>
>> Regards
>> Chris Saunders
>
> x = mt[mti];
>
> Then the ++ is performed.

Actually, no. The ++ is performed first, as part of evaluating 'mti++'.
However, the result of that expression is the former value of mti, i.e. the
one before evaluating that expression.


#include <stdio.h>
int g = 0;
void foo(int x)
{
printf("x=%d, g=%d\n", x, g);
}
int main()
{
foo(g++);
}
// output: x=0, g=1

Note: I'm not 100% sure if this is a proof, it might actually invoke
implementation-specific behaviour or even undefined behaviour. However, the
point I was trying to make was that the definition of the postfix increment
operator rather involves storing the original value in a temporary and not
postponing the increment operation. This isn't visible in the original
example though.

Uli

--
C++ FAQ: http://parashift.com/c++-faq-lite

Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932