| | |
even this simple code?? c'mon c++
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Dec 2008
Posts: 21
Reputation:
Solved Threads: 1
I have this code:
in my judgement it should print 1 and 2, instead it prints 0 and 1
some advice pls... Im using dev c++
in my judgement it should print 1 and 2, instead it prints 0 and 1
some advice pls... Im using dev c++
C++ Syntax (Toggle Plain Text)
#include <iostream> class A { public: A() { arr = new int[2]; } ~A(){ delete [] arr; } int * arr ; }; int main() { int * n ; {//induced code block A aObj; aObj.arr[0] = 1; aObj.arr[1] = 2; n = aObj.arr; /* BTW I assume n now points to the same add of aObj.arr ... am I wrong?? */ } std::cout << "\n" << (*n) << " " << (*n+1); system("pause"); }
Last edited by Ancient Dragon; May 11th, 2009 at 12:20 am. Reason: add line numbers
The problem is scoping -- object A goes out of scope before the cout statement on line 31. Delete the { and } on lines 20 and 29 and your program will work as you expect it to.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: Jan 2008
Posts: 3,820
Reputation:
Solved Threads: 501
Keep in mind that this line (see red part):
displays
std::cout << "\n" << (*n) << " " << (*n+1);displays
aObj.arr[0] + 1 , not aObj.arr[1] . In your case, they are the same, but if you change line 23 to equal something other than 2, you'll still get 1 2 as output. Last edited by VernonDozier; May 11th, 2009 at 12:33 am.
![]() |
Similar Threads
- Help newbie : need simple code (Python)
- Simple code is confusing me (Python)
- Need simple code for VB.NET (VB.NET)
- Uploading File Not Working...simple code, what's wrong? (PHP)
- simple code (C++)
- CAn anyone provide me a very simple code for shopping cart? (PHP)
- Simple (I think) Code Question (PHP)
Other Threads in the C++ Forum
- Previous Thread: Updating prices in a 2d array
- Next Thread: Inputting data into C ...
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game generator getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






