Thread: How did you master pointers?

  1. #1
    Registered User Afrinux's Avatar
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    46

    How did you master pointers?

    No matter, how hard and how long I have studied the pointers, I always have problems with them when I start coding.
    I know the basics. I know what they mean.
    But I always get confused, when passing pointers to other functions;
    when searching the memory using the pointers; when incrementing them, decreasing them.
    My question is what did you do to master them? Is there any good books to help master them for good?
    Thanks in advance.

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    No matter, how hard and how long I have studied the pointers, I always have problems with them when I start coding.
    Don't feel too bad -- I think we all have that problem at times.
    what did you do to master them?
    Years and years of practice.

  3. #3
    Registered User Afrinux's Avatar
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    46
    Years and years of practice.
    Oh, well
    Thanks !

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660

  5. #5
    ex-DECcie
    Join Date
    Dec 2005
    Posts
    125
    In addition to what Salem just suggested (good tutorials are always helpful), in the past I have found it helpful to sit down and try it out, in a very simple way.

    Write some programs. For example, if you're not sure how to pass a pointer into a function, write a program and try it out. Find a good debugger or use printf statements to give you an idea of what is happening.

    When you've got something that works, comment the hell out of it, so that you know exactly what is going on...

    Read other people's code -- well, folks who you know are good programmers, anyway. If you see something you don't understand, dig in and work it out.

  6. #6
    Rabble Rouser Slacker's Avatar
    Join Date
    Dec 2005
    Posts
    116
    >How did you master pointers?
    Heheh, master pointers. Some people might tell you that I've mastered pointers, but some people will say anything. Personally, I don't think it's possible to "master" anything non-trivial, and anyone that says they've mastered pointers is probably so full of themselves that they make rookie mistakes all the time.

    >My question is what did you do to master them?
    Practice, read, ask questions. Nobody knows everything, and while mastery is a nice goal, you should be aware that you probably won't reach it and to make the most of what you know now instead of always falling victim to "the grass is always greener" syndrome. Just do your thing and experience will do the rest.

  7. #7
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    A lot of concepts don't fully make sense to me until I've heard them explained in several ways. What I do (and did, with pointers) is just search for a whole bunch of tutorials and eventually my perception of what they and how to use them gets little adjustments until I get the whole idea.

  8. #8
    Registered User Afrinux's Avatar
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    46
    Thank you guys for all the good advices.
    Salem , thanks for the link. I have downloaded the pdf file. Things get tricky on the pointers to functions' chapter. I feel I have a lot to learn.
    When you've got something that works, comment the hell out of it, so that you know exactly what is going on...
    I will keep this in mind. It can be very useful.There are times, I forget the meaning of my own code.
    Read other people's code -- well, folks who you know are good programmers, anyway. If you see something you don't understand, dig in and work it out.
    A friend told me the same thing. I am not good at reading someone else code. When I dont understand something, I get annoyed and just leave it.
    Do you a way to understand someone's code without getting annoyed?
    Practice, read, ask questions, I will try harder.
    Speaking of which, I have one question regarding pointers: a pointer to a pointer. Whats the use of it? In which cases do we need a pointer to a pointer?
    Code:
    int *ptr, **p2tr, addr;
    ptr = &addr; <---- ptr points to  the address of addr
    p2tr = &addr;<----- Is it correct? If so, what does it mean?
    Thanks for taking your time to read this.
    Afrinux

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > p2tr = &addr;<----- Is it correct? If so, what does it mean?
    No, but p2tr = &ptr; would be.

    Code:
    int *ptr, **p2tr, addr;
    ptr = &addr;
    p2tr = &ptr;
    
    // these all assign a value to addr
    addr = 1;
    *ptr = 1;
    **p2tr = 1;
    
    // & and * cancel out, so you can do stuff like this
    *&addr = 1;
    Arrays and pointers are closely linked (though not identical), and can use either notation
    Code:
    char c[10];
    char *p;
    
    // point to start of array
    p = c;
    p = &c[0];
    p = c + 0;
    
    // point to middle of array
    p = &c[5];
    p = c + 5;
    
    // access an element of the array
    c[0] = 'a';
    *(c+0) = 'a';
    p[0] = 'a';
    *(p+0) = 'a';
    For structures, use the -> when dealing with pointers
    Code:
    struct pt { int x; int y };
    struct pt point;
    struct pt *p = &point;
    
    // update via pointer uses any of these
    p->x = 1;    // the usual way
    (*p).x = 1;  // the long-winded way
    p[0].x = 1;  // the obtuse way ;)
    > Things get tricky on the pointers to functions' chapter.
    Yeah, the trick to dealing with them is to use typedefs. Using them is very easy, but the declarations are something else to behold!.
    Also, this site is dedicated to this singularly unique topic.
    http://www.function-pointer.org/

  10. #10
    Registered User
    Join Date
    Mar 2005
    Posts
    135

    might help:P

    Quote Originally Posted by Afrinux
    Speaking of which, I have one question regarding pointers: a pointer to a pointer. Whats the use of it? In which cases do we need a pointer to a pointer?
    Code:
    int *ptr, **p2tr, addr;
    ptr = &addr; <---- ptr points to  the address of addr
    p2tr = &addr;<----- Is it correct? If so, what does it mean?
    Thanks for taking your time to read this.
    Afrinux

    Allow me to explan,

    ::Explanation of differences between pointers to pointers and pointers::

    int addr;

    int *p = &addr;


    What is returned here to 'p' is not a pointer, but an address to some allocated memory storage. p, itself is the pointer. That's a big difference. An address does not automatically make it a pointer unless it was declared as a pointer in the first place.

    When people say, p is a pointer to an int. They're really infering that p is a holder to some normal non-pointer memory.

    - pointers can only hold normal - non pointer - memory (address).

    - pointers to pointers can only hold a special kind of memory - pointer memory (an address):


    *The underlying memory of addr is normal memory
    *The underlying memory of p is pointer memory.

    *The contents (what is hold) of addr is integer values; numbers.
    *The contents (what is hold) of p is memory values; addresses.



    int *p2p = &p;

    what is returned here to 'p2p' is a pointer, which is an address to some allocated memory storage. And that is why it is called a pointer to a pointer.

    The underlying memory (address) of addr and p are both addresses (but quite different), one of them is int memory - normal kind, while, the other is pointer memory - special kind, respectively.





    ::Explanation of pointers::
    when you see a function like:

    void SomeFunction(char *);

    which expects a pointer to a char memory.

    What's really happening is that, it is expecting to be passed some normal memory:


    char array[] = {"constant literal"};

    - The call:
    SomeFunction(array);

    - Definition:
    SomeFuntion(char *p)
    {
    // do your thing...
    }



    p is now a pointer (a holder) to some normal memory.
    the Identifier, 'p' has it's own memory (the underlying memory), which is pointer memory.





    How do you master pointers? Don't know if that's possible, lol. But the gist is to Practice, than practice some more, and more practice, followed even more practice... well, you get the point:P


    xeddiex.

  11. #11
    ex-DECcie
    Join Date
    Dec 2005
    Posts
    125
    Quote Originally Posted by Afrinux
    I will keep this in mind. It can be very useful.There are times, I forget the meaning of my own code. A friend told me the same thing. I am not good at reading someone else code. When I dont understand something, I get annoyed and just leave it.
    Do you a way to understand someone's code without getting annoyed?
    I had a mentor when I was just a junior programmer type who had a habit of asking me "How bad do you want it?" and that usually got me over the frustration of reading someone else's code.

    And a particularly tough piece of code is always a challenge to me -- the more complex it gets, the more I just HAVE to know what the heck is going on. I'll make diagrams, write notes, and become almost like an archeologist digging through ruins.

    The cool part is that every once in a while, when the light goes on and you finally understand what is happening, you come to the realization that you could have coded that particular thing better. It's kinda fun when that happens.

    And lastly, if you're reading someone's code and you don't understand a section of it, and you know the person, just ask them. Most folks (unless they are incurable prima donas) like to talk about code they've written

  12. #12
    Disrupting the universe Mad_guy's Avatar
    Join Date
    Jun 2005
    Posts
    258
    what did you do to master them?
    I haven't.

    However, what always helps me: when dealing with pointers, draw a lot. This isn't a joke or anything - by drawing out your problems with things as problematic as pointers, you can make your life a lot easier (or so I've found.)
    operating systems: mac os 10.6, debian 5.0, windows 7
    editor: back to emacs because it's more awesomer!!
    version control: git

    website: http://0xff.ath.cx/~as/

  13. #13
    Registered User Afrinux's Avatar
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    46
    Thanks guys!
    I have read salem and xeddiex examples. At first glance, I have to admit it, I havent understand everything yet. But I will make an effort to understand both explanations and when I get stuck again, I will come back to ask questions.
    Quote Originally Posted by Salem
    int *ptr, **p2tr, addr;
    ptr = &addr;
    p2tr = &ptr;
    Here I understand that a pointer to a pointer has to point to another pointer's address.
    Quote Originally Posted by xeddiex
    int *p2p = &p;
    what is returned here to 'p2p' is a pointer, which is an address to some allocated memory storage. And that is why it is called a pointer to a pointer.
    If I understand, a pointer can also point to a pointer's address. Then what is the use of pointer to a pointer, if a normal pointer can point to another pointer's address?
    Comparing salem's example and xeddiex's example:
    A pointer can point to a variable address ( ptr = &addr; ) as well to another pointer address (int *p2p = &p; ).
    Will this pointer (int *p2p = &p; ) have the same function as the pointer (int **p2p2 = &p )?
    Or did xeddiex mean (int **p2p = &p; )? See how fast I get confused

    fgw_three and Mad_guy, thanks again. I will try drawing as well.
    Afrinux
    Last edited by Afrinux; 01-15-2006 at 11:08 PM.

  14. #14
    Registered User
    Join Date
    Mar 2005
    Posts
    135
    Quote Originally Posted by Afrinux
    Thanks guys!
    I have read salem and xeddiex examples. At first glance, I have to admit it, I havent understand everything yet. But I will make an effort to understand both explanations and when I get stuck again, I will come back to ask questions. Here I understand that a pointer to a pointer has to point to another pointer's address.
    If I understand, a pointer can also point to a pointer's address. Then what is the use of pointer to a pointer, if a normal pointer can point to another pointer's address?
    Comparing salem's example and xeddiex's example:
    A pointer can point to a variable address ( ptr = &addr; ) as well to another pointer address (int *p2p = &p; ).
    Will this pointer (int *p2p = &p; ) have the same function as the pointer (int **p2p2 = &p )?
    Or did xeddiex mean (int **p2p = &p; )? See how fast I get confused

    fgw_three and Mad_guy, thanks again. I will try drawing as well.
    Afrinux

    Ok, first off, It's normal to be confused, you have to in order to "master" pointers:P. Else you're probably some super geeky weirdo who'd I wouldn't wanna socialize with (just joking).

    Like I stated in my first post: The address of a pointer variable (pointers are variables too) is not the same as the address of a non-pointer variable. A pointer with only 1 asterisk can only hold an address (variable, array etc,) that is not a pointer. A pointer with 2 asterisk can only hold an address of a pointer which was declared as as a, pointer (char*).

    char **p2p;
    char *p = &p2p; // no-no
    char *p = p2p; // no-no

    char variable;
    char *p = &variable; // ok

    [BTW: p2p stands for pointer to pointer so don't think I'm declaring it as a normal pointer, which I would declare as, p]


    xeddiex.

  15. #15
    ex-DECcie
    Join Date
    Dec 2005
    Posts
    125
    Quote Originally Posted by Afrinux
    Thanks guys!
    I Then what is the use of pointer to a pointer, if a normal pointer can point to another pointer's address?

    fgw_three and Mad_guy, thanks again. I will try drawing as well.
    Afrinux

    First off, you're welcome. Along the way I've had many folks who've helped me to learn things, and I've always felt the best way to thank them is to pass knowledge on in like manner.....

    This probably won't make much sense to you, but I'm at work and don't have the time to make a proper example, but a pointer to a pointer is useful is you need to pass a pointer by reference into a function.

    Suppose you have a function that, among other things, malloc's some memory. If you malloc the memory inside the function, and have associate it with a pointer defined inside that function, the pointer will disappear when the function returns. (The malloc-ed memory won't, and that would be a memory leak, but that's a different story).

    So, you pass the ADDRESS of a pointer into your function, and associate the malloc-ed memory with that pointer (that is defined outside your function).

    Thus, you need a pointer to a pointer.....

    Hope that helps a little....

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