29Jun

Help with LINKED LIST please?

1 comment so far


I was designing a phonebook in C with Linked list as my data structure. There is a ’store’ function that Stores a Name along with its Number. I am passing node *head directly. This gives me a segmentation fault. However if I pass node **head and use *head to control the list It works fine.
But passing node *head and using head to control the list, it does not work.

Same with Creating Empty list:

This does NOT work.
void createEmptyList(node *head)
{
head=NULL;
}

This WORKS:

void createEmptyList(node **head)
{
*head=NULL;
}

WHY is this happening?? Shouldn’t both do the same thing.

This is the structure I am using:
typedef struct nodeType
{
char name[30];
char num[10];
struct nodeType *next;
} node;

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • StumbleUpon
  • Reddit
  • Webnews
  • MisterWong
  • Y!GG
Categories: Programming & Design

Tuesday, June 29th, 2010 at 6:21 am and is filed under Programming & Design. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

One Response to “Help with LINKED LIST please?”

  1. Posted by Tizio 008 29th June, 2010 at 7:11 am

    absolutely not.
    in the first case, you’re nullifying just the “local copy” of a variable head, which is a pointer to a node… but anyway, you’re modifying just the local copy of a pointer; this do not affect the passed in pointer.

    the second case it’s the right one: you indeed pass a pointer (to a pointer to node), and you use that pointer to modify the “original” variable.

    maybe you can see it more easily scaling down; let us suppose you have a function like

    void myfunc(int a)
    {
    a = 10;
    }

    /*. … */
    int p;
    myfunc(p);

    this func does nothing at the “original” variable (p) passed: it modify the local copy which it’s discarded when the func ends.

    if you want to modify the original, you must do

    void myfunc(int *a)
    {
    *a = 10;
    }

    /** … */
    int p;
    myfunc(&p);

    now, instead of the value (”content”) of the integer p, you pass the pointer to the memory area where the value is stored.
    the function see that pointer as a pointer to a integer (int * in the C syntax).
    now we can dereference the pointer to modify directly the memory area where p is stored, and this is what we do with
    *a = 10;
    the pointer a is used to “reach” the memory area of p.
    the local variable now is just a pointer… we can modify it of course, just for fun since it won’t do anything at all

    void myfunc(int *a)
    {
    *a = 10; // modify what’s pointed by a, i.e. p…
    a = (int *)0×40008000; // modify the pointer…
    // the pointer it’s a local variable, so nothing special
    // happens but…
    *a = 20;
    // of course now the pointer is “invalid”, i.e. pointing
    // to something we maybe do not own, so the previous
    // line cause an error: we are deferencing the modified
    // copy of our variable (which by the way is a pointer)
    }

    instead of int, you can put anything else; in particular, another pointer, e.g. if we have

    struct node *head;

    to modify this from within a function we must pass the pointer, &head… which gives as the “odd” type struct node **head, i.e. a pointer which point to a variable which is a pointer to a struct node.

Leave a reply