pointers

This is a discussion on pointers within the C++ Programming forums, part of the General Programming Boards category; I decided to to read some of the tutorials again to clarify stuff I missed and such. Code: #include <iostream> ...

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    465

    Wink pointers

    I decided to to read some of the tutorials again to clarify stuff I missed and such.

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    { 
      int x;            // A normal integer
      int *p;           // A pointer to an integer
    
      p = &x;           // Read it, "assign the address of x to p"
      cin>> x;          // Put a value in x, we could also use *p here
      cin.ignore();
      cout<< *p <<"\n"; // Note the use of the * to get the value
      cin.get();
    }
    Alright how I understand this is p is a pointer due to the *. p points to the address of x because of the &. So becuase p points to x it should display the value of x?
    My computer is awesome.

  2. #2
    Pokemon Master digdug4life's Avatar
    Join Date
    Jan 2005
    Location
    Mystic Island, NJ
    Posts
    91
    What?

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    x is the name of a memory location. That location holds an int.

    p is the name of another memory location. That location holds an address. When you write "p=&x" you are storing the address of memory location "x" in memory location "p".
    When you write "cout<<x" you are sending whatever's stored in location x to the outstream.
    When you write "cout<<*p" you are getting the address that's stored in location p, going to that address, and sending whatever's stored at that address to the outstream.

  4. #4
    Pokemon Master digdug4life's Avatar
    Join Date
    Jan 2005
    Location
    Mystic Island, NJ
    Posts
    91
    ok still not clear

    If u havent relized, im not very good at this

  5. #5
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    yes. Hopefully this will show it a little better:
    Code:
    #include <iostream>
    
    using std::cin;
    using std::cout;
    using std::endl;
    
    int main() {
    
    	int x;
    	int* p;
    
    	p = &x;
    
    	cout << "Enter a value for x: ";
    	cin >> x;
    	cin.ignore();
    
    	cout.setf(std::ios_base::hex);
    	cout << "Address of x: " << &x << endl;
    	cout << "value of p: " << p << endl;
    
    	cout.setf(std::ios_base::dec);
    	cout << "Value of x: " << x << endl;
    	cout << "Value that p points to: " << *p << endl;
    
    	cin.get();
    	return 0;
    }
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  6. #6
    Pokemon Master digdug4life's Avatar
    Join Date
    Jan 2005
    Location
    Mystic Island, NJ
    Posts
    91
    But that made a little sense

  7. #7
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    Alright thanks for explaining it.
    My computer is awesome.

  8. #8
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    I didn't want to make a new post just for this, so how many of the tutorials do you think I should read before I start working again? I just read the one about arrays and its starting to get confusing.
    My computer is awesome.

  9. #9
    Registered User
    Join Date
    Jul 2004
    Posts
    53
    Read and ask questions until you aren't confused anymore...

  10. #10
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    Yet another victory for me!

    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        int number = rand()%30;
        int 
        guess=-1;
        int trycount=0;
        
        while(guess != number && trycount < 8)
        {
        cout<<"Please enter you guess:";
        cin>>guess;
        if (guess<number)
        cout<<"too low"<<endl;
        if (guess>number)
        
        cout<<"too high"<<endl;
        trycount++;
        }
        if (guess==number)
        cout<<"you go it!";
        else
        cout<<"Looks like you guessed wrong. The number was:"<<number;
        cin.ignore();
        cin.get();
        }
    This is the guessing game challenge someone directed me to. I changed a few things and peeked on the else, but otherwise I got it.
    My computer is awesome.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  2. function pointers
    By benhaldor in forum C Programming
    Replies: 4
    Last Post: 08-19-2007, 10:56 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 12:18 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21