Thread: about pointers!!!

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    80

    Post about pointers!!!

    for example

    Code:
    createnode(mynodetype **p)
    {
       *p = (mynodetype * )malloc(sizeof(mynodetype));
       *p
       ...
    
    main() 
    {
        mynodetype **a;
        createnode(a);
    }
    in this code why do i have to use **p?cant it be *p??

  2. #2
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    you should be doing

    Code:
    createnode(mynodetype **p)
    {
       *b = (mynodetype * )malloc(sizeof(mynodetype));
       *b
       ...
    
    main() 
    {
        mynodetype* a;
        createnode(&a);
    }
    Otherwise you will be dereferencing an uninitialized memory address.

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    80

    thx

    but why do we use **p rather than *p

  4. #4
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Because p is a pointer to a pointer to a mynodetype. Dereferencing it once will give you a pointer to a mynodetype, while dereferencing it twice will give you the mynodetype being pointed to by the pointer whose memory address is stored in p.

  5. #5
    Registered User
    Join Date
    Jan 2002
    Posts
    80

    thx

    thx for ur answer

  6. #6
    Registered User Xei's Avatar
    Join Date
    May 2002
    Posts
    719
    Why not use:
    Code:
    CreateNode(MyNodeType *p)
    {
         p = (MyNodeType *)malloc(sizeof(MyNodeType));
    }
    main()
    {
         MyNodeType *p;
         CreateNode(p);
         return 0;
    }
    ?

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Xei
    Why not use:
    [--snip--]
    ?
    You use a pointer to a pointer when there is the potential need to update what you're originally pointing to. Consider:
    Code:
    struct node *list;
    
    void myFun( struct node ** listptr )
    {
        ...do stuff...
        *listptr = newnode;
    }
    
    
    mFun( &list );
    You can now pass this function an empty list and have it update the original pointer. Without a pointer to a pointer, all you can do is something like:

    list = myFun2( ... );

    Where myFun2 returns the pointer.

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

  8. #8
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Originally posted by Xei
    Why not use:
    Code:
    CreateNode(MyNodeType *p)
    {
         p = (MyNodeType *)malloc(sizeof(MyNodeType));
    }
    main()
    {
         MyNodeType *p;
         CreateNode(p);
         return 0;
    }
    ?
    Because that would cause a memory leak and you'd lose the memory location of the allocated memory once you leave the function. p in main would not be pointing to the newly allocated memory. You have to pass p by reference via a pointer to a pointer as was shown in example.

  9. #9
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    The original code is being used wrongly.

    If you want change where a pointer is pointing by use of a function, there are two ways to do it nl.

    1. Pass the pointer by reference.
    2. Return the new pointer from the function.

    Example:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void createnode1(int ***p)
    {
    	*p = (int**)malloc( sizeof(int*) );
    }
    
    int** createnode2(int **p)
    {
    	p = (int**)malloc( sizeof(int*) );
    	return p;
    }
    
    int main(int argc, char* argv[])
    {
    	int **a = NULL;
    	int **b = NULL;
    
    	printf("a = %p\n", a);
    	createnode1(&a);
    
    	printf("a = %p\n", a);
    	
    	printf("b = %p\n", b);
    	b = createnode2( b );
    	printf("b = %p\n", b);
    
    	free(a);
    	free(b);
    
    	fgetc(stdin);
    	return 0;
    }
    In the original code, the function accepted a "**", and a "**" was passed to it. So the new pointer must either be returned or the pointer must be passed by reference.

  10. #10
    Registered User Xei's Avatar
    Join Date
    May 2002
    Posts
    719
    This seems very mentally taxing at the moment...(either that, or it's the 5.5 hours I just spent in a small room with 40 people learning NOTHING just to get a insurance discount). I shall find more material explaining this, if you know of any links, please post them. Thanks.

    edit: nm, I get it, I'm just not thinking.
    Last edited by Xei; 04-13-2003 at 05:00 PM.

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