[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c

increment operator at left hand side of equation.

Naresh

5/4/2011 4:34:00 AM

Hi all,

Is this valid in as per C standard ? --> *(pt++) = temp;

int *ptr;
int temp = 0;
*(pt++) = temp;

Would there be any confusion to compiler about which operation should
it do first? increment or assignment ?

Thanks,
Nishu
4 Answers

Naresh

5/4/2011 4:46:00 AM

0

On May 4, 9:33 am, Nishu <naresh.at...@gmail.com> wrote:

> *(pt++) = temp;

Can I treat the above statement as invalid in accordance to below
faq ?

http://c-faq.com/expr/evalo...

Thanks,
Nishu

China Blue Veins

5/4/2011 4:49:00 AM

0

In article <51514726-2803-447b-9639-e84d16564a58@z13g2000prk.googlegroups.com>,
Nishu <naresh.attri@gmail.com> wrote:

> Hi all,
>
> Is this valid in as per C standard ? --> *(pt++) = temp;

Yes.

> int *ptr;
> int temp = 0;
> *(pt++) = temp;
>
> Would there be any confusion to compiler about which operation should
> it do first? increment or assignment ?

The result of pt++ will always be the value of pt before the increment, and the
value of the subexpression *pt++ will be computed before the assignment. However
the sequencing of the assignment of pt+1 to pt relative to assignment of temp to
*pt is undefined; since the value of the subexpression (temp) doesn't depend on
the value of (pt), and vice versa, this is a safe assignment.

--
Damn the living - It's a lovely life. I'm whoever you want me to be.
Silver silverware - Where is the love? At least I can stay in character.
Oval swimming pool - Where is the love? Annoying Usenet one post at a time.
Damn the living - It's a lovely life. Why does Harmony have blue veins?

Naresh

5/4/2011 4:54:00 AM

0

On May 4, 9:48 am, China Blue Veins <chine.b...@yahoo.com> wrote:

> > Is this valid in as per C standard ? -->  *(pt++) = temp;
>
> Yes.

> > Would there be any confusion to compiler about which operation should
> > it do first? increment or assignment ?
>
> The result of pt++ will always be the value of pt before the increment, and the
> value of the subexpression *pt++ will be computed before the assignment. However
> the sequencing of the assignment of pt+1 to pt relative to assignment of temp to
> *pt is undefined; since the value of the subexpression (temp) doesn't depend on
> the value of (pt), and vice versa, this is a safe assignment.


Thank you. I understood you point. I also read http://c-faq.com/expr/con....
Nishu

Peter Nilsson

5/4/2011 5:50:00 AM

0

Nishu <naresh.at...@gmail.com> wrote:
> Is this valid in as per C standard ? -->  *(pt++) = temp;
>
> int *ptr;
> int temp = 0;
> *(pt++) = temp;

No, for two reasons. Firstly, pt is not declared or defined
anywhere. If you meant ptr, then still no since ptr has no
initial value. You're incrementing an undetermined value.

> Would there be any confusion to compiler about which
> operation should it do first? increment or assignment ?

The general case you're hinting at is fine, although the
following would be problematic...

int temp = 0;
int *ptr = &temp;
*(ptr++) = temp++;

....because temp is modified twice between sequence points.

--
Peter