943,677 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3075
  • C++ RSS
May 31st, 2004
0

Why am I getting this output

Expand Post »
Why is the last element not 7

C++ Syntax (Toggle Plain Text)
  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!!!!!!
Similar Threads
TJW
Reputation Points: 11
Solved Threads: 0
Newbie Poster
TJW is offline Offline
12 posts
since May 2004
May 31st, 2004
0

Re: Why am I getting this output

You've got a 3-element array. Why are you trying to manipulate 4 elements?
C++ Syntax (Toggle Plain Text)
  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.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Jun 1st, 2004
0

Re: Why am I getting this output

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
Reputation Points: 108
Solved Threads: 7
Posting Whiz in Training
FireNet is offline Offline
256 posts
since May 2004
Jun 1st, 2004
0

Re: Why am I getting this output

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.
TJW
Reputation Points: 11
Solved Threads: 0
Newbie Poster
TJW is offline Offline
12 posts
since May 2004
Jul 4th, 2006
-1

Re: Why am I getting this output

Quote originally posted by TJW ...
Why is the last element not 7

C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Light Poster
joshilay is offline Offline
31 posts
since Jul 2006
Jul 4th, 2006
1

Re: Why am I getting this output

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jul 8th, 2006
-1

Re: Why am I getting this output

Quote originally posted by TJW ...
Why is the last element not 7

C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sagar arora is offline Offline
4 posts
since Jul 2006
Jul 8th, 2006
0

Re: Why am I getting this output

The question was already answered in this old, and now closed, thread.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004

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.
This thread is currently closed and is not accepting any new replies.
Previous Thread in C++ Forum Timeline: C++ help
Next Thread in C++ Forum Timeline: A little help again





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


Follow us on Twitter


© 2011 DaniWeb® LLC