944,111 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1399
  • C++ RSS
Aug 24th, 2005
0

Pointers

Expand Post »
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;
}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Busted is offline Offline
2 posts
since Mar 2005
Aug 24th, 2005
0

Re: Pointers

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.
Reputation Points: 19
Solved Threads: 5
Junior Poster
Stoned_coder is offline Offline
164 posts
since Jul 2005
Aug 24th, 2005
0

Re: Pointers

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,961 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Whats wrong with this class???
Next Thread in C++ Forum Timeline: code seems to stop executing





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC