Will I ever need "pointer of type pointer to int?"

This is a discussion on Will I ever need "pointer of type pointer to int?" within the C Programming forums, part of the General Programming Boards category; Will I ever use "Pointer of type pointer to int?" Or any other type like double, char or whatever? And ...

  1. #1
    dat
    dat is offline
    Registered User
    Join Date
    Nov 2001
    Posts
    28

    Will I ever need "pointer of type pointer to int?"

    Will I ever use "Pointer of type pointer to int?" Or any other type like double, char or whatever? And if so, when and where?

    Thanks,

    Dat
    Dat
    http://hoteden.com <-- not an porn site damnit!

  2. #2
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    when you need it, be ready to use it. but they are not the most common pointers. i've used char **foo a bunch with text parsing stuff.
    hello, internet!

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    It is sometimes useful in linked lists. If you want to modify the previous node's NextNode pointer from the current node, you need to get the adress of that pointer to change it from within the previous node (Did that make sense? Well, I tried... ).
    Code:
    //Not good, only the local pointer is changed
    void SetNextNode(NODE* Node)
    {
       Node = this->NextNode;
    }
    
    //Works better
    void SetNextNode(NODE** Node)
    {
       *Node = this->NextNode;
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    dat
    dat is offline
    Registered User
    Join Date
    Nov 2001
    Posts
    28
    Originally posted by Magos
    It is sometimes useful in linked lists. If you want to modify the previous node's NextNode pointer from the current node, you need to get the adress of that pointer to change it from within the previous node (Did that make sense? Well, I tried... ).
    Code:
    //Not good, only the local pointer is changed
    void SetNextNode(NODE* Node)
    {
       Node = this->NextNode;
    }
    
    //Works better
    void SetNextNode(NODE** Node)
    {
       *Node = this->NextNode;
    }
    Huh?
    Dat
    http://hoteden.com <-- not an porn site damnit!

  5. #5
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    You'll obviously need a pointer if you want to pass a pointer by reference as in Magos' example.

    Here's the same thing, but you might grasp it better :
    Code:
    int OpenFile(FILE** fp, char* fileName, char* mode)
    {
          if ( (*fp = fopen(fileName, mode)) == NULL )
          {
                return -1;
          }
          return 0;
    }
    If the parameter fp was just a FILE*, then the stream would only be accessible within the function OpenFile(), and not outside it.

  6. #6
    Bios Raider biosninja's Avatar
    Join Date
    Jul 2002
    Location
    South Africa
    Posts
    765
    Huh?
    it's part of a linked list.

    Study linked lists, very cool and usefull

  7. #7
    FCF
    FCF is offline
    Registered User FCF's Avatar
    Join Date
    Dec 2001
    Posts
    40
    Study linked lists, very cool and usefull
    I agree.

    Currently i am studying linked list, there are two different implementations-- array-based and use malloc()'s linked list(sorry dunno the name). My question is:
    When i use the array-based linked list, i usually define a constant first like #define MAX_NODE 999. u know, the constant is fixed so if the data exceeds the MAX_NODE, the linked list can't handle the data. The conclusion is: array-based linked list is not so good. Am i right?

  8. #8
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Originally posted by FCF
    The conclusion is: array-based linked list is not so good. Am i right?
    Depends. Everything has its good and bad sides. Having a static array as the list consumes unused memory if you have little data, and might not be large enough if you have a lot of data.
    On the other hand, you can't directly access a specific element using a dynamic linked list, though you can theoretically store an unfinite amount of data.

    If you know exactly how many elements you need to store, use arrays. If you don't know (you might add or delete during run-time) use a dynamic linked list.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you check what is wrong with this code
    By Ron in forum C++ Programming
    Replies: 4
    Last Post: 08-01-2008, 11:59 PM
  2. strings Vs. Char pointers
    By aijazbaig1 in forum C Programming
    Replies: 49
    Last Post: 02-13-2008, 09:51 AM
  3. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 04:36 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 02:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21