Thread: This Program may help you to understand pointer

  1. #1
    Registered User
    Join Date
    Jul 2014
    Location
    Central Arizona
    Posts
    61

    This Program may help you to understand pointer

    Hi,
    I'm new to C++. I wrote this simple program to help other understand the basic of Pointer. I has not been proofread but I hope it will help you.

    Code:
    /* this program demonstrates the use of a pointer the variables x and y are printed then the pointer gets the address where each are stored. It prints that addresses. The pointer is then refererenced by *pname and gets the value of each variable through the pointer  */
    
     #include <iostream>
     using namespace std;
     int main ()
     {
       int x = 3;  //declare and initialize the variables
       int y = 7;
       int *pname;  //declare a pointer
       cout << x << endl; //prints 3 the value held in x
       pname = &x; //the pointer gets address where x is stored
       cout << pname << endl; // print the address where x is stored
       cout << * pname << endl; //prints the value held in x
       cout << y << endl; //prints 7 the value held in y
       pname = &y; //pointer gets address where y is stored
       cout << pname << endl; //prints the address where y is stored
       cout << *pname << endl; //prints the value held in y
       return 0;
     }

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    It's fine, I'd just add some empty lines for readability. Try to organize the code in blocks, it's easier to comprehend that way. Like, for example:
    Code:
    /* this program demonstrates the use of a pointer the variables x and y
    are printed then the pointer gets the address where each are stored. It prints
    that addresses. The pointer is then refererenced by *pname and gets the value
    of each variable through the pointer  */
     
    #include <iostream>
    
    using namespace std;
    
    int main ()
    {
        int x = 3;   //declare and initialize the variables
        int y = 7;
        int *pname;  //declare a pointer
    
        cout << x << endl; //prints 3 the value held in x
        pname = &x;        //the pointer gets address where x is stored
    
        cout << pname << endl;   // print the address where x is stored
        cout << * pname << endl; //prints the value held in x
    
        cout << y << endl; //prints 7 the value held in y
        pname = &y;        //pointer gets address where y is stored
    
        cout << pname << endl;  //prints the address where y is stored
        cout << *pname << endl; //prints the value held in y
    
        return 0;
    }
    Devoted my life to programming...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help me understand what the next program does
    By alon4963 in forum C Programming
    Replies: 4
    Last Post: 10-26-2013, 11:26 PM
  2. Can you help me understand this program?
    By SCRIPT_KITTEH in forum C Programming
    Replies: 7
    Last Post: 07-23-2013, 04:03 AM
  3. Help for understand pointer
    By redred in forum C Programming
    Replies: 8
    Last Post: 01-19-2013, 01:41 PM
  4. Don't understand backtrace on invalid pointer
    By SterlingM in forum C++ Programming
    Replies: 5
    Last Post: 09-21-2011, 02:00 PM
  5. Can someone help me understand this example program
    By Guti14 in forum C Programming
    Replies: 6
    Last Post: 09-06-2004, 12:19 PM

Tags for this Thread