Thread: Whats a Pointer

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    219

    Whats a Pointer

    Thanks To All Today I've understood properly whats Pointer. I was little bit confused about it before
    Code:
    Today I've Understood Compleately Whats Pointer
    "Pointer Is a Special Type Of Variable"
    First lets Discuss about Some General Variables
    		int var = 50;
    Here a Memory address Say 0xbf45d2c5 has been Alloted for this variable.
    And in that Location a int Value has been Stored thats 50
    and Thats All Now Lets Come To pointer
    		int *pVar;
    		*pVar = var;
    "pVar Points to var" Here pVar is a Pointer Variable
    And value of pVar is the Address of teh Variable named var
    
    
       (var name)pVar				    (var name)var	
    ---------------------				---------------------
    | (value)0xb7d58bf1 | 			        |     (value) 50    | 
    ---------------------				---------------------	
    (address) 0x4cf8a7d2e				(address) 0xb7d58bf1
    
    So pVar is like a tree it holds another variable's Address and that variable
    holds the value . So how Would we get the value under the pVar 
    (Thats at teh End of the Tree) So as the Value is on depth 1 we need to use
    		cout<<*pVar
    To get teh value of var from pVar Directly So we can call * as a direct access 
    to value Operator or Syntax for this time just to understand Correctly.
    If pVar Points/holds another pointer variable names say mVar and mVar Points/holds 
    the variable then we need to use
    		cout<<**pVar
    To get the value of var from pvar Directly as teh depth is now 2. If we use *pVar
    It will just print the Address of var . As the value of mVar is teh Address of var.
    Am I right ??
    Last edited by noobcpp; 06-30-2007 at 05:45 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    An illustration of the relationship between & and * when referring to a pointer variable.
    Code:
    #include <iostream>
    using namespace std;
    
    int main( void ) {
        int var = 1234;
        int *pVar = &var;
        cout << reinterpret_cast<void*>(&pVar) << endl;
        cout << reinterpret_cast<void*>(pVar) << endl;
        cout << *pVar << endl;
        return 0;
    }
    
    
     Address  Contents
             +--------+
    0x22cce0 |0x22cce4| pVar
             +--------+
    0x22cce4 |  1234  | var
             +--------+
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jun 2007
    Posts
    219
    So was I right ??

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Barring a few syntactic problems, your explanation seems OK.

    Though 'pointer to pointer to' is more akin to a list rather than a tree.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Jun 2007
    Posts
    219
    K Thanks
    But Whats
    reinterpret_cast<void*>

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  2. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 03:47 PM
  3. Parameter passing with pointer to pointer
    By notsure in forum C++ Programming
    Replies: 15
    Last Post: 08-12-2006, 07:12 AM
  4. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM