Thread: Confusion with pointers

  1. #1
    Registered User Stormboy's Avatar
    Join Date
    Aug 2013
    Location
    Planet Earth
    Posts
    12

    Confusion with pointers

    Hi,
    I am almost going to finish the C Tutorials. But I still am not clear with one thing. That is Pointers. I get what pointers are. But I don't get why, how and when do we use them. I tried reading the Pointers lesson again and again but still I don't get it. Someone please explain it to me.

    I know what are pointers:
    They are like a place holder to a memory location.

    I know how to declare pointers:
    Code:
    int *foo;
    I know how to assign a memory location of a variable to it:
    Code:
    int *foo;
    int bar = 10;
    foo = &bar;
    And that's all.
    So I need to know why, how and when do we use them. Also, how to not use them.

  2. #2
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Short version: parameters for C functions are "pass by value", so whenever you need a function that modifies one of the parameters you'll need a pointer.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    1) If you want a function to change its argument, that argument must be passed as a pointer.

    Code:
    #include <stdio.h>
    
    void try_A(int i)
    {
        i = 42;
    }
    
    void try_B(int *i)
    {
         *i = 10;
    }
    
    int main()
    {
          int x = 2, y = 3;
          try_A(x);
          try_B(&y);
          printf("x = %d, y = %d\n", x, y);
    }
    If you run this code, it will report x has a value of 2 (as the change made by try_A() is invisible to main()) and y has a value of 10 (since try_B() changed it in a manner visible to main()).

    2) Pointers are a building block that allows a program to dynamically allocate and manage memory. Imagine you know you need an array, but the size can only be determined by data obtained by the program when run, and the function which allocates that array needs to provide that array to its caller.

    3) [Note this is a slightly more advanced reason]. In general, a pointer is an abstraction that can be used to iterate over memory. They can be used to iterate over elements of an array. They also make it much easier to implement certain data structures, such as linked lists.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Quote Originally Posted by grumpy View Post
    1)
    3) [Note this is a slightly more advanced reason]. In general, a pointer is an abstraction that can be used to iterate over memory. They can be used to iterate over elements of an array. They also make it much easier to implement certain data structures, such as linked lists.
    What?! Pointers are not an abstraction; they're a solid concept.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by SirPrattlepod
    What?! Pointers are not an abstraction; they're a solid concept.
    Since the C standard does not require that the underlying hardware and machine language have the same notion of "pointer" with value, type, and set of operations as it is known in C, I'd say that grumpy is right: a pointer is an abstraction. It sounds as if you think that "abstraction" comes with a negative connotation, but it doesn't.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by laserlight View Post
    Since the C standard does not require that the underlying hardware and machine language have the same notion of "pointer" with value, type, and set of operations as it is known in C, I'd say that grumpy is right: a pointer is an abstraction. It sounds as if you think that "abstraction" comes with a negative connotation, but it doesn't.
    I wouldn't bother debating with SirPrattlePod, laserlight. He (I assume) is on my ignore list (so I only saw the post you responded to because you quoted it) for a reason - being unnecessarily argumentative for the sake of being argumentative, mostly from positions of excessive ignorance coupled with arrogance.

    But you've captured my point 100% correctly. A pointer, as specified in the C standard, is an abstraction. An abstraction is a construct with general qualities or characteristics that are useful for reasoning about something of use. That is a definition straight from an english-language dictionary.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  7. #7
    Registered User Stormboy's Avatar
    Join Date
    Aug 2013
    Location
    Planet Earth
    Posts
    12
    Thanks guys. I think I get pointers now.

  8. #8
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Quote Originally Posted by laserlight View Post
    Since the C standard does not require that the underlying hardware and machine language have the same notion of "pointer" with value, type, and set of operations as it is known in C, I'd say that grumpy is right: a pointer is an abstraction. It sounds as if you think that "abstraction" comes with a negative connotation, but it doesn't.
    I agree with your argument. But you have misunderstood me... I don't think that "abstraction" is a negative connotation at all (the thought had never entered my mind until you planted it there, hehe). I don't look at pointers as an abstraction any more than I look at addition or subtraction as abstractions, although in the context of your reply (that I agree with) they are. Everything in C is an abstraction, and modern x86 machine language instructions are an abstraction... where does the rabbit hole end?!

  9. #9
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Quote Originally Posted by grumpy View Post
    I wouldn't bother debating with SirPrattlePod, laserlight. He (I assume) is on my ignore list (so I only saw the post you responded to because you quoted it) for a reason - being unnecessarily argumentative for the sake of being argumentative, mostly from positions of excessive ignorance coupled with arrogance.

    But you've captured my point 100% correctly. A pointer, as specified in the C standard, is an abstraction. An abstraction is a construct with general qualities or characteristics that are useful for reasoning about something of use. That is a definition straight from an english-language dictionary.
    You, Mr Grumpy, are the arrogant one.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. confusion with pointers
    By baxy in forum C Programming
    Replies: 2
    Last Post: 12-01-2011, 06:25 AM
  2. Confusion With Pointers
    By ObjectWithBrain in forum C Programming
    Replies: 28
    Last Post: 07-14-2011, 11:44 AM
  3. Confusion using pointers
    By kluxy in forum C Programming
    Replies: 10
    Last Post: 03-27-2010, 12:07 AM
  4. array and pointers confusion
    By int80 in forum C Programming
    Replies: 5
    Last Post: 05-02-2008, 03:55 AM
  5. Confusion about pointers
    By rishiputra in forum C Programming
    Replies: 1
    Last Post: 05-01-2003, 04:39 PM