suprising output !!

O

onkar

void print(int *a,int *b,int *c,int *d,int *e){
printf("++ptr=%d\nptr--=%d\nptr=%d\nptr++=%d\n++ptr=%d
\n",*a,*b,*c,*d,*e);
}
int main(void){
static int arr[]={97,98,99,100,101,102,103,104};
int *ptr=arr+1;
print(++ptr,ptr--,ptr,ptr++,++ptr);
return 0;
}


gives me ::

++ptr=100
ptr--=100
ptr=100
ptr++=99
++ptr=100

I am totally foxed by this answer. Please try to explain why the
answer must be like that ??
 
J

John Bode

void print(int *a,int *b,int *c,int *d,int *e){
printf("++ptr=%d\nptr--=%d\nptr=%d\nptr++=%d\n++ptr=%d
\n",*a,*b,*c,*d,*e);}

int main(void){
static int arr[]={97,98,99,100,101,102,103,104};
int *ptr=arr+1;
print(++ptr,ptr--,ptr,ptr++,++ptr);
return 0;

}

gives me ::

++ptr=100
ptr--=100
ptr=100
ptr++=99
++ptr=100

I am totally foxed by this answer. Please try to explain why the
answer must be like that ??

You have invoked undefined behavior. Please read the following:

http://www.c-faq.com/expr/evalorder2.html
 
K

Kenneth Brody

onkar wrote:
[...]
print(++ptr,ptr--,ptr,ptr++,++ptr); [...]
I am totally foxed by this answer. Please try to explain why the
answer must be like that ??

For the exact same reasons given in your "tricky output" thread from
last week. You have invoked undefined behavior by modifying ptr
more than once between sequence points.

(And note that there is no "comma operator" in the above statement,
which would have been a sequence point.)

And note that the output doesn't "must be" like that. It happens to
be that way on your particular compiler, with the particular set of
options you compiled it. With UB, _anything_ could happen.

My compiler, with default settings, gives:
++ptr=100
ptr--=99
ptr=99
ptr++=99
++ptr=99

With optimization on, it gives:
++ptr=100
ptr--=99
ptr=100
ptr++=99
++ptr=100

Note that _both_ sets of output are "correct" as far as the Standard
is concerned.

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:[email protected]>
 
M

Malcolm McLean

onkar said:
void print(int *a,int *b,int *c,int *d,int *e){
printf("++ptr=%d\nptr--=%d\nptr=%d\nptr++=%d\n++ptr=%d
\n",*a,*b,*c,*d,*e);
}
int main(void){
static int arr[]={97,98,99,100,101,102,103,104};
int *ptr=arr+1;
print(++ptr,ptr--,ptr,ptr++,++ptr);
return 0;
}


gives me ::

++ptr=100
ptr--=100
ptr=100
ptr++=99
++ptr=100

I am totally foxed by this answer. Please try to explain why the
answer must be like that ??
This one seems to be going the rounds.
For convenience, increments can be written as ++ rather than += 1. For
convenience also, the pre-increment is applied first and the postincrement
last.

However if you also assign to the incremented value in the same expression,
there's a problem.

y = x++;

means

y = x;
x++;

is
x = x++;

meant to mean

x = x + 1;

or

x = x?

There's no sensible answer, so the expression is undefined. This is also to
help out the compiler writers a bit - it's the sort of thing that might be
hard to catch in parser.
 
P

Peter Nilsson

onkar said:
void print(int *a,int *b,int *c,int *d,int *e){
        printf("++ptr=%d\nptr--=%d\nptr=%d\nptr++=%d
\n++ptr=%d\n",*a,*b,*c,*d,*e);}

int main(void){
        static int arr[]={97,98,99,100,101,102,103,104};
        int *ptr=arr+1;
        print(++ptr,ptr--,ptr,ptr++,++ptr);
        return 0;
}

gives me ::

++ptr=100
ptr--=100
ptr=100
ptr++=99
++ptr=100

I am totally foxed by this answer.

Why do you expect to receive a different answer to
same question you asked a few days ago?

Tell us what aspects of previous answers (e.g. the FAQ)
are still causing confusion.
 

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

Forum statistics

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

Latest Threads

Top