Newbie needs help with pointers

This is a discussion on Newbie needs help with pointers within the C++ Programming forums, part of the General Programming Boards category; Bah, pointers. I have come to the chapter in my book were I learn about pointers. I don't understand a ...

  1. #1
    using namespace Trooper; St0rmTroop3er's Avatar
    Join Date
    Sep 2003
    Posts
    77

    Newbie needs help with pointers

    Bah, pointers. I have come to the chapter in my book were I learn about pointers. I don't understand a single bit about em. Can someone help, and explain them? Thanks. Could you explain why this code works?

    Code:
    int main(int argc, char *argv[])
    {
    int intVar;
    int* pintVar;
    
    pintVar = &intVar;
    *pintVar = 10;
    
    cout << pintVar <<  " is pIntVar\n";
    cout << intVar <<  "is intVar\n";
    
      
      return 0;
    }
    All i get is pintVar as a hex and intVar as 10. I don't understand whats going on, how, and why. If anyone could help, that would be great. BTW, I"m using dev-c++

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Instead of storing a value, a pointer stores the address to another variable (or some junk data if you're unlucky).
    pintVar = &intVar;
    Here you tell the pointer to point at the variable intVar. & returns the address of a variable.
    *pintVar = 10;
    Here you set what the pointer points at to 10. It points to intVar, so you set intVar to 10. The * tells that you set the value to what it points at. If you just wrote pintVar = 10; you would change the actual address the pointer points at to some probably invalid value.
    cout << pintVar
    Here you print the value of the pointer, which is the address it's pointing at. They are often expressed as hex numbers, as you've experienced.

    To print the variable it points at, type:
    cout << *pintVar
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    using namespace Trooper; St0rmTroop3er's Avatar
    Join Date
    Sep 2003
    Posts
    77
    so is it the same thing as saying pintvar = intVar; ?

  4. #4
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,904
    pintVar is equal to the address of intVar, or:
    pintVar equals &intVar

    whereas *pintVar equals intVar, because when you use *pintVar, it is just like you had intVar in there.

    So *pintVar==10 (or *pintVar==intVar), where pintVar==&intVar

    meh, i hope that didn't just confuse you more
    Last edited by jverkoey; 06-02-2004 at 07:20 PM.

  5. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    so is it the same thing as saying pintvar = intVar; ?
    No. pintvar holds the address of a variable that contains and int.

    Think of it like this. Say you have a cat, I want you to show me where the cat is. So you point to the cat. All you are doing is telling me the location of the cat. You are the pointer. Through you I can get to the cat and make changes (aka a haircut).

    so:
    Code:
     int *pintvar;
    Is you
    Code:
     int intVar;
    is the cat
    Code:
     &intVar;
    is the location of the cat
    Code:
     pintvar = &intVar;
    is having you point to the cat's location
    Code:
     *pintvar = ANNOY_KITTY;
    is me going through you to get to the cat and make a change.

  6. #6
    using namespace Trooper; St0rmTroop3er's Avatar
    Join Date
    Sep 2003
    Posts
    77
    ok, get it!

  7. #7
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    lol @ Thantos, that's the best explanation I've heard
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  8. #8
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Quote Originally Posted by Thantos
    Code:
     *pintvar = ANNOY_KITTY;
    is me going through you to get to the cat and make a change.
    hahaha... best code evar... great explanation too...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  9. #9
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    St0rmTroop3er,

    Just in-case you are wondering why anyone would want to use a pointer, it's usually to get-around the fact the a function can only return one value. So, if you want your function to affect more than one variable, you can use pointers. There is probably an example or exercise in your book that uses pointers to "swap" the values of two variables.

    Pointers to arrays, structures, and objects are very common because they give you access to all of the variables & data they contain.

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, 06:59 AM
  2. delete and delete[]
    By Hunter2 in forum C++ Programming
    Replies: 13
    Last Post: 06-26-2003, 05:40 AM
  3. pointers, functions, parameters
    By sballew in forum C Programming
    Replies: 3
    Last Post: 11-11-2001, 10:33 PM
  4. Pointers pointers pointers....
    By G'n'R in forum C Programming
    Replies: 11
    Last Post: 11-02-2001, 02:02 AM
  5. Pointers pointers pointers...
    By SMurf in forum C Programming
    Replies: 8
    Last Post: 10-23-2001, 05:55 PM

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