| | |
Why am I getting this output
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: May 2004
Posts: 12
Reputation:
Solved Threads: 0
Why is the last element not 7
Output
00044
1003<-----------------Why 3 and not 7!!!!!!
C++ Syntax (Toggle Plain Text)
int array[3]; int *ary; ary=array; array[0]=0; array[1]=0; array[2]=0; array[3]=44; for (int i=0;i<4;i++) { cout<<array[i]; } cout<<endl; *(ary+0)=1; *(ary+1)=0; *(ary+2)=0; *(ary+3)=7; for (int i=0;i<4;i++) { cout<<array[i]; } cout<<endl;
Output
00044
1003<-----------------Why 3 and not 7!!!!!!
You've got a 3-element array. Why are you trying to manipulate 4 elements?
C++ Syntax (Toggle Plain Text)
#include <iostream> int main() { int array[] = {1,2,3}, *ptr = array; for ( std::size_t i = 0; i < sizeof array / sizeof *array; ++i ) { std::cout << "array [ " << i << " ] = " << array[i] << std::endl; } for ( std::size_t i = 0; i < sizeof array / sizeof *array; ++i ) { std::cout << "*(ptr + " << i << ") = " << *(ptr + i) << std::endl; } return 0; } /* my output array [ 0 ] = 1 array [ 1 ] = 2 array [ 2 ] = 3 *(ptr + 0) = 1 *(ptr + 1) = 2 *(ptr + 2) = 3 */
Last edited by The Other Dave; May 31st, 2004 at 5:47 pm. Reason: Added code sample.
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
[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
•
•
Join Date: May 2004
Posts: 12
Reputation:
Solved Threads: 0
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.
Thanks.
•
•
Join Date: Jul 2006
Posts: 31
Reputation:
Solved Threads: 0
•
•
•
•
Originally Posted by TJW
Why is the last element not 7
C++ Syntax (Toggle Plain Text)
int array[3]; int *ary; ary=array; array[0]=0; array[1]=0; array[2]=0; array[3]=44; for (int i=0;i<4;i++) { cout<<array[i]; } cout<<endl; *(ary+0)=1; *(ary+1)=0; *(ary+2)=0; *(ary+3)=7; for (int i=0;i<4;i++) { cout<<array[i]; } 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
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.
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.
•
•
Join Date: Jul 2006
Posts: 4
Reputation:
Solved Threads: 0
•
•
•
•
Originally Posted by TJW
Why is the last element not 7
C++ Syntax (Toggle Plain Text)
int array[3]; int *ary; ary=array; array[0]=0; array[1]=0; array[2]=0; array[3]=44; for (int i=0;i<4;i++) { cout<<array[i]; } cout<<endl; *(ary+0)=1; *(ary+1)=0; *(ary+2)=0; *(ary+3)=7; for (int i=0;i<4;i++) { cout<<array[i]; } 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
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
![]() |
Similar Threads
- Capturing Console Output (C)
- Reverse Output (Stack) (C++)
- Application Output Help (Java)
Other Threads in the C++ Forum
- Previous Thread: C++ help
- Next Thread: A little help again
| Thread Tools | Search this Thread |
api array arrays based beginner binary bmp c++ c/c++ calculator char class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file format forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib library linkedlist linker linux list loop looping loops map math matrix memory newbie news number output pointer problem program programming project python random read recursion recursive reference return rpg simple string strings studio temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets





