:confused: Could any one justify why is the result of the following code 100 100 100 99 99??

main()
{

static int arr[]={97,98,99,100,101,102,103,104};
int *ptr=arr+1;

print(++ptr, ptr--, ptr ,ptr++ ,++ptr);

}

print( int *a, int *b, int *c, int *d, int *e)
{
cout<<*a<<" "<<*b<<" "<<*c<<" "<<*d<<" "<<*e<<endl;
}

Recommended Answers

All 2 Replies

it isnt. its undefined behaviour. your compiler could say that its anything. You modify a variable more than once in a given expression without a sequence point hence undefined behaviour.

The results of that program is unpredictable, undefined, and compiler implementation dependent. So one compiler may give one result while another compiler may give other results. why? because the compiler may, or may not, push the results of the pointer operations onto the stack after each operation. Its entire possible and conceivable that only the final result of ptr will get pushed onto the stack before print function is called.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.