Why am I getting this output

Please support our C++ advertiser: Intel Parallel Studio Home
Closed Thread

Join Date: May 2004
Posts: 12
Reputation: TJW is an unknown quantity at this point 
Solved Threads: 0
TJW TJW is offline Offline
Newbie Poster

Why am I getting this output

 
0
  #1
May 31st, 2004
Why is the last element not 7

  1. int array[3];
  2. int *ary;
  3.  
  4. ary=array;
  5. array[0]=0;
  6. array[1]=0;
  7. array[2]=0;
  8. array[3]=44;
  9. for (int i=0;i<4;i++)
  10. {
  11. cout<<array[i];
  12. }
  13. cout<<endl;
  14.  
  15.  
  16. *(ary+0)=1;
  17. *(ary+1)=0;
  18. *(ary+2)=0;
  19. *(ary+3)=7;
  20. for (int i=0;i<4;i++)
  21. {
  22. cout<<array[i];
  23. }
  24. cout<<endl;

Output
00044
1003<-----------------Why 3 and not 7!!!!!!
Quick reply to this message  
Join Date: Apr 2004
Posts: 4,400
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 247
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Why am I getting this output

 
0
  #2
May 31st, 2004
You've got a 3-element array. Why are you trying to manipulate 4 elements?
  1. #include <iostream>
  2. int main()
  3. {
  4. int array[] = {1,2,3}, *ptr = array;
  5. for ( std::size_t i = 0; i < sizeof array / sizeof *array; ++i )
  6. {
  7. std::cout << "array [ " << i << " ] = " << array[i] << std::endl;
  8. }
  9. for ( std::size_t i = 0; i < sizeof array / sizeof *array; ++i )
  10. {
  11. std::cout << "*(ptr + " << i << ") = " << *(ptr + i) << std::endl;
  12. }
  13. return 0;
  14. }
  15. /* my output
  16. array [ 0 ] = 1
  17. array [ 1 ] = 2
  18. array [ 2 ] = 3
  19. *(ptr + 0) = 1
  20. *(ptr + 1) = 2
  21. *(ptr + 2) = 3
  22. */
Last edited by The Other Dave; May 31st, 2004 at 5:47 pm. Reason: Added code sample.
Quick reply to this message  
Join Date: May 2004
Posts: 256
Reputation: FireNet will become famous soon enough FireNet will become famous soon enough 
Solved Threads: 6
FireNet's Avatar
FireNet FireNet is offline Offline
Posting Whiz in Training

Re: Why am I getting this output

 
0
  #3
Jun 1st, 2004
Oh, my poor child :-),
[just kidding].
If you have an array like array[3] you can use only 3 elements i.e
1. array[0]
2. array[1]
3. array[2]

If you go beyound that you will get errors as you would be writing over memory used by some other program.

so array[3] is the fouth element.so declare it as int array[4].Should solve you prob
See what you can, remember what you need

Fourzon | Earn via Coding
Quick reply to this message  
Join Date: May 2004
Posts: 12
Reputation: TJW is an unknown quantity at this point 
Solved Threads: 0
TJW TJW is offline Offline
Newbie Poster

Re: Why am I getting this output

 
0
  #4
Jun 1st, 2004
Wow. I completely missed that. That was a typo. I am bad. I got so focused on why my ouput was wrong and looked at my pointer etc and completely ignored the array declaration. HAHAHAHAHA. Oh well!!!! One note, apparently Borland does not optimize your code as Visual C++ because I would get the correct output on Visual C++ and not Borland Builder 6.

Thanks.
Quick reply to this message  
Join Date: Jul 2006
Posts: 31
Reputation: joshilay is an unknown quantity at this point 
Solved Threads: 0
joshilay joshilay is offline Offline
Light Poster

Re: Why am I getting this output

 
-1
  #5
Jul 4th, 2006
Originally Posted by TJW
Why is the last element not 7

  1. int array[3];
  2. int *ary;
  3.  
  4. ary=array;
  5. array[0]=0;
  6. array[1]=0;
  7. array[2]=0;
  8. array[3]=44;
  9. for (int i=0;i<4;i++)
  10. {
  11. cout<<array[i];
  12. }
  13. cout<<endl;
  14.  
  15.  
  16. *(ary+0)=1;
  17. *(ary+1)=0;
  18. *(ary+2)=0;
  19. *(ary+3)=7;
  20. for (int i=0;i<4;i++)
  21. {
  22. cout<<array[i];
  23. }
  24. cout<<endl;

Output
00044
1003<-----------------Why 3 and not 7!!!!!!
hey ..d ans is very simple d array shld be declare as array[4] nt as array[3] ..dis is solve it
Quick reply to this message  
Join Date: Sep 2004
Posts: 7,783
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 744
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Why am I getting this output

 
1
  #6
Jul 4th, 2006
>hey ..d ans is very simple d array shld be declare as array[4] nt as array[3] ..dis is solve it
Not only are your grammar and spelling atrocious, this thread is over two years old. Look at the date of the thread before you post in it, please. And keep in mind that not all of our members speak English fluently, and your silly abbreviations could be terribly confusing.
Last edited by Narue; Jul 4th, 2006 at 12:47 pm.
I'm here to prove you wrong.
Quick reply to this message  
Join Date: Jul 2006
Posts: 4
Reputation: sagar arora is an unknown quantity at this point 
Solved Threads: 0
sagar arora sagar arora is offline Offline
Newbie Poster

Re: Why am I getting this output

 
-1
  #7
Jul 8th, 2006
Originally Posted by TJW
Why is the last element not 7

  1. int array[3];
  2. int *ary;
  3.  
  4. ary=array;
  5. array[0]=0;
  6. array[1]=0;
  7. array[2]=0;
  8. array[3]=44;
  9. for (int i=0;i<4;i++)
  10. {
  11. cout<<array[i];
  12. }
  13. cout<<endl;
  14.  
  15. *(ary+0)=1;
  16. *(ary+1)=0;
  17. *(ary+2)=0;
  18. *(ary+3)=7;
  19. for (int i=0;i<4;i++)
  20. {
  21. cout<<array[i];
  22. }
  23. cout<<endl;

Output
00044
1003<-----------------Why 3 and not 7!!!!!!

hi man.i have tried this,the output comes out to be ooo44,1007 not 1004 u have to try this again and see.i think its problem of ur compiler.but as i think it is right as 1007
Quick reply to this message  
Join Date: Apr 2004
Posts: 4,400
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 247
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Why am I getting this output

 
0
  #8
Jul 8th, 2006
The question was already answered in this old, and now closed, thread.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Quick reply to this message  
Closed Thread

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



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC