C++ question 8 replies

Please wait...

Scijox

I miss the store...

50 XP

3rd January 2004

0 Uploads

553 Posts

0 Threads

#1 14 years ago

[EDIT] If you cant see the code just highlight it [/EDIT] Ok I am sorry if this question is noobish at abb but I am still in the learning process of C++. I have made 2 programs using dev-Cpp (I have all the standard librarys). The firs one looks like this #include #include using namespace std; int main(int argc, char *argv[]) { int num = 1; cout << "Declared" << endl; cout << num << endl; // making pointer for num int *ptrnum; ptrnum = &num; cout << "address: " << ptrnum << endl; // displaying address *ptrnum = 2; // ^^ simply madifing the num variable using a pointer cout << "Modified by this program through using a pointer" << endl; cout << num << endl; cout << "------------------------------------------------" << endl; cout << "You may now launch access program" << endl; cout << "Pointer address = " << *ptrnum << endl; // no more modification by this program system("PAUSE"); cout << "------------------------------------------------" << endl; cout << num << endl; cout << "------------------------------------------------" << endl; cout << "THE NUMBER DISPLAYED ABOVE WAS NOT MODIFIED BY THIS PROGRAM" << endl; system("PAUSE"); return 0; } Thats all fine and good but my goad is to use a second program to modify the variable num through its address. Its address is 0x22ff6c so knowing that I went out to make another program and it goes like this. #include #include using namespace std; int main(int argc, char *argv[]) { cout << "Modifing 0x22ff6c to = o" << endl; cout << "----------------------------------------------------" << endl; int num; int *pointer1; pointer1 = 0x22ff6c; cout << "Address: " << pointer1 << endl; system("PAUSE"); *pointer1 = 3; cout << "Done. 0x22ff6c is: " << num << endl; system("PAUSE"); return 0; } But this does not work I get an error that reads "Invalid conversion from 'int' to 'int' How do I specify the address I want without the variable in my program? Now this is where it gets weird and why I am so clueless. Look at these two programs. #include #include using namespace std; int main(int argc, char *argv[]) { int num1=1; // print addresses cout << &num1 << endl; // program is still running so they should stay on the stack right or am I wrong? system("PAUSE"); return 0; } #include #include using namespace std; int main(int argc, char *argv[]) { int num1=1; // print addresses cout << &num1 << endl; // program is still running so they should stay on the stack right or am I wrong? system("PAUSE"); return 0; } They both declare variables and display the address but the addresses are the same each:deal: . (They are the same program too) Now how can two variables be stored at the same address :confused:. Or are they not on the stack anymore. Or am I missing the point entirly and do I need to store the on the Heap as a global variable. Some sample code would be wonderful but an answer would really help as well. Thx:)




suzukiltd

I'm too cool to Post

50 XP

7th November 2003

0 Uploads

1,649 Posts

0 Threads

#2 14 years ago

Well, I dont really understand cpp since I never bother learning it. I will someday though perhaps. I connect you with a person that knows c++ pretty well though. If you want his email, Ill send it to you.




Scijox

I miss the store...

50 XP

3rd January 2004

0 Uploads

553 Posts

0 Threads

#3 14 years ago

Yes but only if hes interested in helping. I dont wanna bother people I can probably ask this question on another fourm.




Nemmerle Forum Mod

Voice of joy and sunshine

298,655 XP

26th May 2003

0 Uploads

28,168 Posts

6 Threads

#4 14 years ago

Well I'm no great digs at pointers and I dont play with C++, only C, but I'll take a stab at it. My comments will be in red

[COLOR=Black] #include #include using namespace std; int main(int argc, char *argv[]) { cout << "Modifing 0x22ff6c to = o" << endl;

cout << "----------------------------------------------------" << endl; [COLOR=Red]If you get into a situation where you use this a lot you might want to take it out into another procedure[/COLOR]

int num; [COLOR=Red]Why have you included this?[/COLOR]

int *pointer1; pointer1 = 0x22ff6c; [COLOR=Red]Try: &pointer1 = 0x22ff6c;[/COLOR]

cout << "Address: " << pointer1 << endl; system("PAUSE"); *pointer1 = 3;

cout << "Done. 0x22ff6c is: " << num << endl; [COLOR=Red]You havent done anything with 'num' so this will read Done. 0x22ff6c is: And then whatever is a the address of num[/COLOR]

system("PAUSE"); return 0; } [/COLOR]

Just a guess though I don't really play with pointers that much.




Scijox

I miss the store...

50 XP

3rd January 2004

0 Uploads

553 Posts

0 Threads

#5 14 years ago

num was just somthing I forgot to delete when I was trying somthing ill try your idea now. [EDIT]Sry but it did not work I get an error message like this 'non-lvalue in assignment' :([/EDIT]




Nemmerle Forum Mod

Voice of joy and sunshine

298,655 XP

26th May 2003

0 Uploads

28,168 Posts

6 Threads

#6 14 years ago

Remove the *pointer1 = 3; line and see what it prints the value at the address as




Scijox

I miss the store...

50 XP

3rd January 2004

0 Uploads

553 Posts

0 Threads

#7 14 years ago

Its address is 0x42b004 and i wanna set it to 0x22ff6c.




RadioactiveLobster Forum Admin

Jeff is a mean boss

566,085 XP

28th July 2002

0 Uploads

53,159 Posts

1,334 Threads

#8 14 years ago

there is a kid here, Tachyon....he knows coding pretty well...havent seen him in a while thou...

http://www.gamingforums.com/member.php?u=81631 PM him


If there is no image, Mikey broke something...



Scijox

I miss the store...

50 XP

3rd January 2004

0 Uploads

553 Posts

0 Threads

#9 14 years ago

Yeah he knwe a lot about that stuff I wonder if he is still around.