pointers?

This is a discussion on pointers? within the C++ Programming forums, part of the General Programming Boards category; What is the diference btw a pointer & a pointer variable? I mean I was confused by the following statement ...

  1. #1
    Registered User tu_user's Avatar
    Join Date
    Jan 2004
    Posts
    36

    Post pointers?

    What is the diference btw a pointer & a pointer variable?

    I mean I was confused by the following statement while reading an article,

    int x, y;

    *yPtr = 7;

    yPtr =&y;

    *yptr and yPtr is confusing.

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    basically, instead of storing a value, a pointer stores an adress to a variable that stores a value.

    Here you set the value yPtr points at
    *yPtr = ...

    Here you set the adress to where yPtr should point. You should not use an arbitrary number here but rather get the address of a variable through the & operator ( &myInt ).
    yPtr = ...

    There is a pointer tutorial on this site:
    http://www.cprogramming.com/tutorial/lesson6.html
    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
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161

    Re: pointers?

    As Magos said, pointers store an address to a variable.

    In your examples:

    yPtr =&y;
    store the address of y into yPtr.
    You can now reference y or *yPtr as the same value.

    *yPtr = 7;
    7 is stored in the location that yPtr points to. IOW y is set to 7.

    By the way, you must specify yPtr:
    int x, y, *yPtr;
    This tells the compiler that yPtr will store an address.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

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

    Pointers can be confusing at first.

    But, pointers are used alot, so you'll get plenty of practice.

    One thing that confused me at first was that the asterisk (*) has a different meaning in a declaration than in an assignment:

    int *yPtr = &y; // This is OK (* means yPtr is a pointer, not dereferenced)
    *yPtr = &y; // Wrong (Address of y assigned to whatever yPtr "points to")
    yPtr = &y; // This is OK (Address of y assigned to yPtr)
    *yPtr = x; // This is OK (* means "dereference". x is assigned to whatever yPtr "points to")

  5. #5
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    I always found it easier to think about it like this:
    Code:
        ...
        int x=10;           //integer x
        int y=10;           //integer y
        int*xptr=new int;   //pointer to new integer
        int*yptr=&y;   //pointer to new integer (same as yptr=&y)
        ...
        *xptr+=5;   //add 5 to xptr's copy of x (xptr only points to the value)
        *yptr+=5;   //add 5 to y (yptr points to the address of y)
        ...
        delete xptr; //because it points to a newly allocated int-size chunk of memory
        *xptr=0; //point it to nothing
        ...
    now when you print it out, it looks liek this:
    Code:
    x:     10
    y:     15
    *xptr: 15
    *yptr: 15
    xptr:  0x...
    yptr:  0x...
    it's like in a function, when you use the ampersand (&) in the calling of the function to change that variables value in main as well... actually, that's exactly what it is, but I don't want to try to confuse you anymore...
    Last edited by major_small; 01-20-2004 at 04:55 PM.
    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

  6. #6
    Registered User tu_user's Avatar
    Join Date
    Jan 2004
    Posts
    36

    Thumbs up

    thanku v m.

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. function pointers
    By benhaldor in forum C Programming
    Replies: 4
    Last Post: 08-19-2007, 11:56 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM

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