Pointers

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2005
Posts: 2
Reputation: Busted is an unknown quantity at this point 
Solved Threads: 0
Busted Busted is offline Offline
Newbie Poster

Pointers

 
0
  #1
Aug 24th, 2005
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;
}
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 164
Reputation: Stoned_coder is an unknown quantity at this point 
Solved Threads: 5
Stoned_coder Stoned_coder is offline Offline
Junior Poster

Re: Pointers

 
0
  #2
Aug 24th, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,472
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1477
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Pointers

 
0
  #3
Aug 24th, 2005
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC