| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
C++ a question conserning POINTERS
hi there im testing with pointers and fully understand what they do what they are and how they work but i got a question about the values of pointers , and why they need to be copied to. if you dont understand my question ill give u a simple program i made for testing this :
// pointer testing #include <iostream> using namespace std; int main () { char *pointer1; char *pointer2; char w1,w2; pointer1 = &w1; pointer2 = &w2; *pointer1 = 'h'; *pointer2 = *pointer1; *pointer1 = 'z'; cout << "(z) "<< w1 << " (h) "<<w2 << endl; cout << pointer1 << endl; cout << pointer2; } this is how i debug this code : char *pointer1; => create a pointer that points to a adress with a value of type 'char' char *pointer2; => create a pointer that points to a adress with a value of type 'char' char w1,w2; => create 2 variables of type 'char ' pointer1 = &w1; => 'pointer1' points to the adress of 'w1' ( adress 800 in runtime for example ) pointer2 = &w2; => 'pointer2' points to the adress of 'w2' ( adress 900 for example ) *pointer1 = 'h'; => the value pointed by pointer1 = 'h' *pointer2 = *pointer1; => the value pointed by pointer 2 equals the value pointed by pointer 1 ( CHECK IF THIS IS RIGHT cuz m not so sure pointer1 = pointer2 => value of pointer1 equals value of pointer 2 ( NOT THE VALUE THAT IS "pointed to" BUT THE VALUE OF THE POINTER ITSELF ) *pointer1 = 'z'; => pointer1 now points to the value of 'z' cout << "(z) "<< w1 << " (h) "<<w2 << endl; => prints out value of w1 and w2 cout << pointer1 << endl; => value of pointer1 cout << pointer2; = value of pointer 2 with the output of : C:\Documents and Settings\Sven\Bureaublad\PS\Cp\made progs\windows\Debug_Build\Con1.exe (h) h (z) z => values of w1(should be h and also is h) and w2(should be z and also is z) zh‚ÿ =>pointer1 value zh‚ÿ => pointer2 value sounds okay right? right! but now there is the part that i dont understand. i thought , well why do i need this code then : pointer1 = pointer2 ? why are the values the pointers needed in this program? so i cut that piece of code out and ran my program again with this strange output: C:\Documents and Settings\Sven\Bureaublad\PS\Cp\made progs\windows\Debug_Build\Con1.exe (h) z (z) h => TURNED ????? z‚ÿ hz‚ÿ => and the pointers values are diffrent ( what is normal ) but why did the values of w1 and w2 turn? i cant figure out ! please look at this for me and atleast try to help ! i worked long on this topic i realy appreciate a reply! |
|
#2
|
||||
|
||||
|
The reason you need the *pointer2 = *pointer1; in this code is because, (and it looks like an example from a book) is because its showing you how pointers can be used by transferring the actual value of pointer1 into pointer2 then re assigning a new value to pointer1. The reason it didn't do the right thing when you took that part out is because you were essentially overwriting the one value and when you tried to print it, it gave you a garbage value. I know you say you already understand pointers and the assignment operator but this link will help you better understand it. I had to read it several times to really understand them thoroughly, and I still have a hard time occasionally understanding them.
http://www.cplusplus.com/doc/tutorial/pointers.html
__________________
---Official Member Of The Itsacon Fan Club--- ![]() Give a man a fish and he will eat for a day. Teach a man to fish and he will sit in a boat all day drinking beer. |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > C++ a question conserning POINTERS |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|