Thread: How did you master pointers?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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.

  2. #2
    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.

  3. #3
    Registered User Afrinux's Avatar
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    46
    Quote Originally Posted by xeddiex
    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).
    hahaha! I might be a geek, but I might come out with a code which can make, any dude takes Angelina Jolie or Jennifer Lopez out. I am sure you will be begging to socialize with me (..., now I am dreaming ).
    Thanks for the detailled explanation. And also thank you fgw_three.
    I tried to have a look at a pointer to a pointer. Well I think it is too early for the challenge. I gotta stick to normal pointers first. And after that, I will give them a re-try.
    Afrinux

  4. #4
    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