The ProgrammersTalk Community
Forum Register Search Today's Posts Mark Forums Read
Register

Go Back   The ProgrammersTalk Community > General Programming > C / C++


Welcome to the The ProgrammersTalk Community forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact us.
Tags: , , , , ,

Reply
 
LinkBack Thread Tools    Display Modes   
  #1 (permalink)  
Old 09-19-2007, 01:32 AM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
Programming Expert
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,110
iTrader: (0)
HelloWorld will become famous soon enoughHelloWorld will become famous soon enoughHelloWorld will become famous soon enough
Icon11 My Pointer Test Result

Here's the code that I use to test pointer:

PHP Code:
#include <iostream>

using namespace std;

int main() {
    
int num 5;
    
cout << num << endl;
    
cout << &num << endl;

    
int *tester = &num;

    
cout << *tester << endl;

    *
tester 6;

    
cout << *tester << endl;
    
cout << num << endl;

    
int *tester2 tester;

    
cout << tester2 << endl;
    
cout << *tester2 << endl;

So can I conclude that pointer CAN'T be a variable, it's only something that can be used to reference the other variable's memory. And when we change this pointer's value to something else, the variable that is pointed to is also changed because this is passed by reference...? Feel free to add some more summary that I can use to get better understanding of how does the pointer works. Moreover, I need to use * sign whenever I use pointer throughout my program..? If I don't use it, it's just going to be the memory of that pointer, so if I want to make another pointer to this pointer, I should do the last step.. (I DON'T NEED TO USE & sign) lol.. Let me know if I missed any test in this process.. thanx

__________________
PHP Code:
System.out.println("Hello World!"); 

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #2 (permalink)  
Old 09-19-2007, 02:35 AM
Lee's Avatar
Lee Lee is offline
PT Staff*
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: Blackpool, UK
Posts: 615
iTrader: (0)
Lee will become famous soon enoughLee will become famous soon enoughLee will become famous soon enoughLee will become famous soon enough
For more Explanation check out... Pointers That has all the information and explanation you will need on pointers and displays easy pictures so you can see how they work.

I tend to use pointers when i need a user defined array, that being array[i] and i is the value from the user, obviously that can not be done in C++ so i use pointers too do that instead.
Reply With Quote
The Following User Says Thank You to Lee For This Useful Post:
HelloWorld (09-23-2007)
  #3 (permalink)  
Old 09-20-2007, 01:01 PM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
Programming Expert
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,110
iTrader: (0)
HelloWorld will become famous soon enoughHelloWorld will become famous soon enoughHelloWorld will become famous soon enough


Above image is pretty nice clarification

__________________
PHP Code:
System.out.println("Hello World!"); 

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #4 (permalink)  
Old 09-20-2007, 02:33 PM
Lee's Avatar
Lee Lee is offline
PT Staff*
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: Blackpool, UK
Posts: 615
iTrader: (0)
Lee will become famous soon enoughLee will become famous soon enoughLee will become famous soon enoughLee will become famous soon enough
Quote:
Originally Posted by HelloWorld View Post


Above image is pretty nice clarification
Those are the images i was on about, they make it really clear so when you make your own examples its easy!
Reply With Quote
The Following User Says Thank You to Lee For This Useful Post:
HelloWorld (09-23-2007)
  #5 (permalink)  
Old 09-27-2007, 03:16 AM
Bench Bench is offline
Full Programmer
Join Date: Jul 2007
Location: UK
Posts: 111
iTrader: (0)
Bench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished road
Quote:
Originally Posted by HelloWorld View Post
So can I conclude that pointer CAN'T be a variable, it's only something that can be used to reference the other variable's memory. And when we change this pointer's value to something else, the variable that is pointed to is also changed because this is passed by reference...? Feel free to add some more summary that I can use to get better understanding of how does the pointer works. Moreover, I need to use * sign whenever I use pointer throughout my program..? If I don't use it, it's just going to be the memory of that pointer, so if I want to make another pointer to this pointer, I should do the last step.. (I DON'T NEED TO USE & sign) lol.. Let me know if I missed any test in this process.. thanx
You are correct in your assertion that a pointer is used to reference other variables or objects in a program, and that modifying a pointer causes a different variable or object to be referenced.


As for your statement "A pointer can't be a variable" - I assume you mean that The identifier (name) for the pointer cannot be used to directly manipulate the referenced variable. Which, again, is correct. The de-reference operator ( * ) must be used to access the referenced variable.


However, in general, a pointer occupies space in memory, and has a data type, therefore a pointer certainly can be a variable itself - It is possible to create a pointer-to-pointer
Code:
int i            = 5;        //type: int
int* p_i         = &i;       //type: pointer (-to-int)
int** p_p_i      = &p_i;     //type: pointer (-to-pointer(-to-int) )
Its important to get out of the mindset where you think of a pointer as being some kind of a mutator for a variable (Which is how alot of people seem to start out), and begin to treat a pointer as a whole new type.

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!

Last edited by Bench : 09-27-2007 at 03:23 AM.
Reply With Quote
Reply


Thread Tools
Display Modes

   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
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -7. The time now is 10:53 PM. Powered by vBulletin
Copyright © 2000 - 2007, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO © 2007 ProgrammersTalk Sedo - Buy and Sell Domain Names and Websites project info: programmerstalk.net Statistics for project programmerstalk.net etracker® web controlling instead of log file analysis


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50