Thread: need help with pointers in C

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    6

    Smile need help with pointers in C

    hi all,
    well this is my first time here, and i have a pretty simple question i think . .
    i have this project to do, in which i have to implement a linked list using pointers. well i have a int*head pointing to the head of list and
    int ** p;
    p = &head;
    while(**p!=NULL) // is this right for going thru the list or something wrong here??


    thanks a lot.
    i would really appreciate any kind of help from anyone.
    fatima

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Okay I would have to see just a little more code to know where your program is breaking. However, I can start off by explaining linked list.

    Code:
    struct LinkedList{
        int vaule;
        struct LinkedList *p;
    };
    Notice that the p member points to another struct LinkedList. You may want to do a search for a more thorough explanation of stacks, queues, and other memory organization techniques that a linked list can be used for creating.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >while(**p!=NULL) // is this right for going thru the list or something wrong here??
    p is a pointer to a pointer, dereferencing it once will give you the pointer it points to and dereferencing it twice will return the contents of the memory. So if p is a pointer to a node the loop should be:
    Code:
    p = head;
    while ( p != NULL )
      p = p->next;
    And if p is a pointer to a pointer to a node it should be:
    Code:
    p = &head;
    while ( *p != NULL )
      *p = (*p)->next;
    -Prelude
    My best code is written with the delete key.

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