increment operator at left hand side of equation.

N

Nishu

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
 
N

Nishu

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/confused.html.
Nishu
 
P

Peter Nilsson

Nishu said:
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.
 

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,763
Messages
2,569,562
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top