Linked list problem..

Discussion in 'C Programming' started by Rufus V. Smith, Jun 29, 2004.

  1. <> wrote in message
    news:...
    > Hi Clcers,
    > I am trying to build a link list and in my first step while
    > creating the list itself, i faltered. The program below
    > gives segmentation fault. Any help in understanding the
    > problem is greatly appreciated.
    >
    > #include<stdio.h>
    > #include<stdlib.h>
    > int main()
    > {
    > struct node
    > {
    > int data;
    > struct node * next;
    > };
    >
    > struct node * head;
    >
    > head->data=10;
    > head->next=(struct node *)malloc(sizeof(struct node));
    > return 0;
    > }
    >
    >
    > Thanks
    > Shan
    >
    >


    You forgot to allocate the head node.

    All you have now (in head) is an uninitialized pointer. As soon as you set
    head->data = 10; you were writing in god-only-knows memory.


    Rufus
    Rufus V. Smith, Jun 29, 2004
    #1
    1. Advertising

  2. Rufus V. Smith

    Guest

    Hi Clcers,
    I am trying to build a link list and in my first step while
    creating the list itself, i faltered. The program below
    gives segmentation fault. Any help in understanding the
    problem is greatly appreciated.

    #include<stdio.h>
    #include<stdlib.h>
    int main()
    {
    struct node
    {
    int data;
    struct node * next;
    };

    struct node * head;

    head->data=10;
    head->next=(struct node *)malloc(sizeof(struct node));
    return 0;
    }


    Thanks
    Shan
    , Jun 29, 2004
    #2
    1. Advertising

  3. Rufus V. Smith

    j Guest

    <> wrote in message
    news:...
    > Hi Clcers,
    > I am trying to build a link list and in my first step while
    > creating the list itself, i faltered. The program below
    > gives segmentation fault. Any help in understanding the
    > problem is greatly appreciated.
    >
    > #include<stdio.h>
    > #include<stdlib.h>
    > int main()
    > {
    > struct node
    > {
    > int data;
    > struct node * next;
    > };
    >
    > struct node * head;
    >
    > head->data=10;


    And just where do you think ``head'' is pointing to?

    > head->next=(struct node *)malloc(sizeof(struct node));
    > return 0;
    > }
    >
    >
    > Thanks
    > Shan
    >
    >
    >
    >
    >
    j, Jun 29, 2004
    #3
  4. Rufus V. Smith

    pete Guest

    wrote:

    > #include<stdio.h>
    > #include<stdlib.h>
    > int main()
    > {
    > struct node
    > {
    > int data;
    > struct node * next;
    > };
    >
    > struct node * head;


    head = malloc(sizeof *head);
    if (head != NULL) {
    head->data = 10;
    printf("%d\n", head->data);
    } else {
    puts("malloc didn't work.");
    }


    > return 0;
    > }


    --
    pete
    pete, Jun 29, 2004
    #4
  5. pete wrote:

    > wrote:
    >
    >> #include<stdio.h>
    >> #include<stdlib.h>
    >> int main()
    >> {
    >> struct node
    >> {
    >> int data;
    >> struct node * next;
    >> };
    >>
    >> struct node * head;

    >
    >
    > head = malloc(sizeof *head);
    > if (head != NULL) {
    > head->data = 10;

    And here you forgot:
    head->next = NULL;
    > printf("%d\n", head->data);
    > } else {
    > puts("malloc didn't work.");
    > }
    >
    >
    >
    >> return 0;
    >> }


    - Dario
    =?UTF-8?B?IkRhcmlvIChkcmlua2luZyBjb++sgGVlIGluIHRo, Jun 29, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris Ritchey
    Replies:
    7
    Views:
    466
    emerth
    Jul 10, 2003
  2. Chris Ritchey

    Generating a char* from a linked list of linked lists

    Chris Ritchey, Jul 9, 2003, in forum: C Programming
    Replies:
    7
    Views:
    455
    emerth
    Jul 10, 2003
  3. fool
    Replies:
    14
    Views:
    494
    Barry Schwarz
    Jul 3, 2006
  4. joshd
    Replies:
    12
    Views:
    656
    John Carson
    Oct 2, 2006
  5. jawdoc
    Replies:
    9
    Views:
    736
    Chris Thomasson
    Mar 10, 2008
Loading...

Share This Page