Thread: Some questions about pointers

  1. #1
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901

    Some questions about pointers

    here is my basic Idea of pointers from what I learned so far. Plz tell me if I am correct and the second part with the pCat is a little confusing.
    Code:
    int * pPoint = 5;    // this deferences a pointer, assigning it 5.
    cout << &pPoint;  // shows the address of the pointer
    
    //some other q's
    int cat=5;
    *pCat = &myAge;
    cout << *pCat;  //now does this return the address of the cat,
                              //or the value 5 which is stored in the address?
    
    //also
    cout << pCat;  // what would that output?
    Last edited by indigo0086; 09-08-2002 at 10:51 AM.

  2. #2
    Registered User
    Join Date
    Aug 2002
    Posts
    23
    Toss it in a complier and find out. Not trying to be rude, but you will find out that when beginning C++ you will do this often. I use VC++ and used to open 2 versions of the program, 1 for the project I was working on, and 1 to test stuff out. I also put cout statments all over the place so I can check values as they run through the program. This is just one way to look for bugs. Good Luck!
    This space for Rent.

  3. #3
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    yeh, I see the output, but I don't see why that is the output.

  4. #4
    Registered User Dr. Bebop's Avatar
    Join Date
    Sep 2002
    Posts
    96
    None of it should work, you can't assign a constant value to a pointer, so

    int * pPoint = 5;

    is wrong. You need to make another variable to hold the 5 and then have the pointer point to the address of that variable.

    int p = 5;
    int * pPoint = &p;

    You need to declare variables before using them, so

    *pCat = &myAge;

    is also wrong. Both pCat and myAge don't exist in the code you posted. Assuming that they both did exist,

    cout << *pCat;

    would print whatever value is in myAge because that is where pCat points to. You dereference the pointer which means "go to the address pointed to and return the contents of it". And

    cout << pCat;

    would print whatever address myAge is located at because cout prints the contents of the variable you pass it, a pointer is a variable that holds an address, so the output would be an address.

    Bebop
    Processing error: Stupidity detected.
    ------------------------------
    Dr. Bebop
    Windows XP Professional Ed.
    Microsoft Visual Studio 6

  5. #5
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    yeah, sorry I didn't declare them, but I was just using them to assume they were. Thanks, I get it now, so if I just wanted to show the address I woud not use (*). If I wanted to get the value at the address I would use the (*) I understand it now.

  6. #6
    Registered User
    Join Date
    Aug 2002
    Posts
    23
    Sorry, I didn't fully understand what you were asking.
    This space for Rent.

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. Replies: 4
    Last Post: 12-10-2006, 07:08 PM
  4. pointers, functions, parameters
    By sballew in forum C Programming
    Replies: 3
    Last Post: 11-11-2001, 10:33 PM
  5. Pointers pointers pointers...
    By SMurf in forum C Programming
    Replies: 8
    Last Post: 10-23-2001, 04:55 PM