C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old May 3rd, 2006, 03:58 PM
ConTagious ConTagious is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2006
Posts: 5 ConTagious User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 19 m 18 sec
Reputation Power: 0
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!

Reply With Quote
  #2  
Old May 3rd, 2006, 06:29 PM
Geo.Garnett's Avatar
Geo.Garnett Geo.Garnett is offline
Registered Loser
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2005
Location: Retardation Nation...
Posts: 347 Geo.Garnett User rank is Private First Class (20 - 50 Reputation Level)Geo.Garnett User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 4 Days 3 h 13 m 45 sec
Reputation Power: 4
Send a message via AIM to Geo.Garnett
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.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > C++ a question conserning POINTERS


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

Request Your Free Technology Downloads!
 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

Request Your Free Technology Downloads!
 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

Request Your Free Technology Downloads!
 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

Request Your Free Technology Downloads!
 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

Request Your Free Technology Downloads!
 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
Stay green...Green IT