Thread: pointers *shudder*

  1. #16
    Registered User
    Join Date
    Aug 2006
    Posts
    11
    Appologies in advance -
    Sightly off topic, but if you could just sit through 5 minutes of this video you'll be a little bit more comfortable with pointers.

    PointerFunC
    32 MB warning.

    Its a video made in stanford university that lol uses animation to teach the nitty gritty of pointers. Helped me a while back.

    if that link doesn't work its here as well, plus other videos made to illustrate pointers but the above link is worth watching for its comic value.

    forum topic on pointers

    enjoy and good luck

  2. #17
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    Quote Originally Posted by SlyMaelstrom
    xPtr is a pointer which accepts the address of an integer. x is an integer, so you pass the address of x (&x) to the pointer.
    If i am passing the address (&x) to the pointer (xPtr), shouldn't the pointer (based on the address passed) determine what value is pointing to and modify the value? Rather than assigning a memory allocation at the start of the program? i.e.
    Code:
     xPtr = &x;
    trickae2,

    thanks i'll have a look.

  3. #18
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    This
    Code:
    xPtr = &x;
    doesn't involve "memory allocation", any more than "x=5" does. It just assigns the address of x to xPtr.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #19
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335

    Unhappy

    Ok, what i meant was that you're assigning x's memory address to xPtr making xPtr point to x.

    That video was excellent. Although it just repeated what i already know i now see pointers in a visual representation that's easy to understand. But i still doesn't clear my doubts about calculate accepting a pointer de-reference and why exactly i'm passing x's address....

  5. #20
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    A pointer is just like any other variable. All it does is hold a value. The value a pointer holds is a memory address. You dereference the pointer to "get to" whatever is being "pointed to".
    Code:
    int x;
    int *p;
    
    x = 5; /* assign some value to x... */
    p = &x; /* assign some value to p... */
    Here you can see they work the exact same way. You simply assign them a value. However, keep in mind the type of value they each expect. One expects an int, so we give it one. The other expects the address of a variable, so we give it one.

    You can also assign the value stored in one pointer to another, just like you can assign the value stored in one "normal" variable to another:
    Code:
    int x, int y;
    int *p, *q;
    
    x = 5;
    p = &x; /* Just like before... */
    
    y = x; /* Assign the value stored in x to y. */
    q = p; /* Assign the value stored in p to q. */
    Everything in C is done 'by value'. When you pass a variable to a function, it makes a copy of that variable, and passes it to the function. In other words, it passes the value stored in the variable to a function, and not the actual variable. Now consider a function call with an int argument:
    Code:
    int foo( int bar )
    {
    }
    
    ...
    
    foo( x );
    This does not send x to the function. It sends the value of x to the function. So in this case, it would send the value 5 to the function. The function creates a variable called 'bar', which is an integer, filled with the value passed to it.

    Likewise, functions that pass pointers pass their values to functions. So if we had this:
    Code:
    void foo( int *bar )
    {
    }
    
    ...
    
    foo( p );
    It would take the value stored in p, which is the address of x, and stick it in a new varaiable foo, when the function is called. Since we now have the address of a variable which exists outside of the function, we can dereference it and change the value of that object. Because when you dereference a pointer, you access the variable it points to. Or rather, the variable at that address.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #21
    Registered User j.sreejit's Avatar
    Join Date
    Aug 2006
    Posts
    10

    Post

    Quote Originally Posted by Axel
    If i am passing the address (&x) to the pointer (xPtr), shouldn't the pointer (based on the address passed) determine what value is pointing to and modify the value? Rather than assigning a memory allocation at the start of the program? i.e.
    Code:
     xPtr = &x;
    The address (&x) is passed to the pointer (xPtr), which is in the function calculate() and the second line in main assigns memory to the pointer xPtr, which is in main.

    I believe these are two different pointer variables with the same name(xPtr). One is local to main and the other is local to the function calculate.
    Code:
    int x = 0;
    int *xPtr = &x; // Make xPtr point to x.
    
    calculate(&x); // Pass a reference to the variable x, which will make xPtr point to x???
    According to the code above both variables xPtr in main and xPtr in calculate point to the same memory location (i.e. address of x), but they are NOT the SAME. Both are NOT ACTIVE at the same time.


    When the address of the variable x is passed to the function it does nothing with the xPtr declared in main. So, if you comment the initialization part in main i.e
    Code:
    //xPtr = &x
    the output of
    Code:
    printf("%d", *xPtr);
    will be unexpected.

  7. #22
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by quzah
    Actually it generates the address of a given variable.


    Quzah.
    Known in programming circles as a pointer

  8. #23
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by esbo
    Known in programming circles as a pointer
    Really? In my circle it's known as the address of the variable... is the address of my house pointing to the house or is a little piece of paper that contains my address on it pointing to my house? I'm sure at some point the address returned by the address of operator is sitting somewhere in memory, but unless you put it somewhere more permanent, it's not really a pointer. That's just my opinion, I can't speak for other posters on this forum.
    Last edited by SlyMaelstrom; 08-29-2006 at 12:06 PM.
    Sent from my iPad®

  9. #24
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Known in programming circles as a pointer
    An address is just an address, but a pointer is a variable capable of holding an address. Saying that an address is a pointer is akin to saying that a bucket is a liquid. Typically, we will blur the difference when talking amongst ourselves, but that's because we really do know the difference and can pull what we need out of the context in which the term is used. However, around here it's best to be as precise as possible to avoid confusion.
    My best code is written with the delete key.

  10. #25
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Its really is very simple, think of the 'Deal or no Deal' game show.

    The pointer is the number on the box.
    The variable is the value in the box.

    By choosing box 15 you 'point to' box 15.

    *box15= £10,000
    &£10,000=box15

    Also if you do *& they cancel each other as does *&*&*&*&*

    & means "the address of" and * means "the contents of the address"

    So *&a means the contents of the address of a, which is a!!!




    So if you have
    int a=10;
    then
    a=10 and *&a=10 and *&*&*&*&*&*&=10

  11. #26
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Its really is very simple
    Then why are you having so much trouble with it?
    My best code is written with the delete key.

  12. #27
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by esbo
    &£10,000=box15
    Nooo.... the address of $10,000 is the address of $10,000. The computer doesn't know that there is some variable(box15) that contains the address. Just because it so happens that box15 holds the address of the money doesn't mean the address of the money is now box15.

    Deal or No Deal has nothing to do with pointers. In reality the suitcases in that show are variables, not pointers. They hold an integer of money. Now, the little que card in Howie Mandel's hand that says which suitcase contains the $1,000,000 is a pointer to that suitcase.
    Sent from my iPad®

  13. #28
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by Prelude
    >Known in programming circles as a pointer
    An address is just an address, but a pointer is a variable capable of holding an address. Saying that an address is a pointer is akin to saying that a bucket is a liquid. Typically, we will blur the difference when talking amongst ourselves, but that's because we really do know the difference and can pull what we need out of the context in which the term is used. However, around here it's best to be as precise as possible to avoid confusion.

    "but a pointer is a variable capable of holding an address"

    I wound not agree with that.

    I would say
    "but a pointer variable is a variable capable of holding an address"

    In my opinion a pointer is not necessarilly a variable.

    I mean an interger can be a variable and often is, but it is not always, sometimes it is
    a constant, ie 5.

  14. #29
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by Prelude
    >Its really is very simple
    Then why are you having so much trouble with it?
    My trouble seems to be with understanding you!!

  15. #30
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by SlyMaelstrom
    Nooo.... the address of $10,000 is the address of $10,000. The computer doesn't know that there is some variable(box15) that contains the address. Just because it so happens that box15 holds the address of the money doesn't mean the address of the money is now box15.

    Deal or No Deal has nothing to do with pointers. In reality the suitcases in that show are variables, not pointers. They hold an integer of money. Now, the little que card in Howie Mandel's hand that says which suitcase contains the $1,000,000 is a pointer to that suitcase.
    No Noel Edmunds presents the show.

    Box 15 is the address of the £10,000 the variable exists in the mind of the contestant.

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, 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