-3
#include<stdio.h>
#include<stdlib.h>

struct node {
    char str[200];
    int nn;
    struct node* next;
};

int number;
struct node* start=NULL;
struct node* current;
//function to insert into the list 

void insert() {
    struct node* n;
    n=(struct node*)malloc(sizeof(struct node));
    n->str=malloc(sizeof(char) * 1000);
    printf("please enter the data that you would like to insert: ");
    gets(n->str);
    printf("asdasdasdasd");
    n->next=NULL;
    if( start==NULL ) {
        start->next=n;
        current=n;
    }
    else {

    current->next=n;
    current=n;

    }

    printf("done\n");
}

void display() {
    current=start;
    int i=0;
    while( current->next!=NULL ) {
        printf("node%d= %s\n",++i,current->str);
        current=current->next;

    }
    printf("this the end");
}


int main() {
    char c;
    int input;

    do {
        printf("Select from the following options:\n"
           "1.Display list\n"
           "2.Add to list\n"
           "3.delete from list\n");

        scanf("%d",&input);
        switch (input) {
            case 1: display(); break;
            case 2: insert(); break;
      //    case 3: delete(); break;
            default : printf("Please select 1 , 2 or 3\n");
        }
        printf( "would you like to continue?(y/n)\n");
        scanf("%s",&c);
    } while(c=='y');

    return 0;
}

This is giving me an error in the insert function, A SEGMENTATION FAULT !

I Tried things but I just don't get the clear picture. I am a bit weak with pointers, actually confused !

Please help me out by telling me what I am doing wrong. Forget about my linked list logic, let it be wrong. I just wanna know why a segmentation fault is occurring !

2
  • 6
    This isn't the code you run and got the segmentation fault from. This code has an assignment to an array which prevents it from compiling.
    – pmg
    Aug 18, 2012 at 20:45
  • 1
    1. Indent your code. 2. Have you managed to compile that? Aug 18, 2012 at 20:46

6 Answers 6

3

you have many mistakes, but your crash is because you don't allocate start

n->next=NULL;
    if( start==NULL ) {
        start->next=n;
        current=n;
    }
    else {

    current->next=n;
    current=n;

you ask if start is null, and if it is you try to read it by calling start->next it. you need to allocate it first.

3

Since you did not tell us the line in which your segfault happened, I can only guess.

At least these lines sounds bogus to me:

if( start==NULL ) {
    start->next=n;
    current=n;
}

Are you sure that you want to dereference start when it is NULL? That is what you are doing with start->next. Dereferencing NULL causes a segfault.

Maybe this will work the way you expect it:

if( start==NULL ) {
    start=n;
    current=n;
}

So basically, if start was not defined yet, define it and it also becomes your current item.

3
  • i was getting an error at the malloc line in insert function, which din make much sense. but what you pointed out is right !! Thanks
    – hunterr986
    Aug 18, 2012 at 20:57
  • Then you could simply change your struct node { char str[200];... to struct node { char* str;.... I was actually wondering at this line: n->str=malloc(sizeof(char) * 1000); but I was not sure if I should have commented on that :P. You declare your str as automatic but you allocate the memory dynamically...
    – xQuare
    Aug 18, 2012 at 21:01
  • Initially i started off with struct node { char* str; ... } but when i was allocating memory using malloc for this char pointer i had some difficulty so i changed it to char array. I will try it again and shall let you know if i am getting an error ...thanks
    – hunterr986
    Aug 19, 2012 at 23:11
2

You should use debugger (not us) in order to find more information about the problem.

For example gdb (GNU Debuger).

2
  1. Compilation Error
    n->str=malloc(sizeof(char) * 1000);

    and this is address reassign on a constant!!!!!You are changing the address of array???

  2. Dangerous Function with 25% probability to execute.

    gets(n->str);

  3. Setting value at null pointer!!!

    struct node* start=NULL;....
    start->next=n;

Example of working Code

  #include<stdio.h>
    #include<stdlib.h>

    struct node {
        char *str;
        int nn;
        struct node* next;
        };

    int number;
    struct node* start=NULL;
    struct node* current;
    //function to insert into the list

    void insert() {
        struct node* n;
        n=(struct node*)malloc(sizeof(struct node));
        n->str=(char*)malloc(sizeof(char) * 1000);
        printf("please enter the data that you would like to insert: ");
        scanf("%s",n->str);
        printf("asdasdasdasd");
        n->next=NULL;
        if( start==NULL ) {
            //start->next=n;
            current=n;
        }
        else {

        current->next=n;
        current=n;

        }

        printf("done\n");
    }

    void display() {
        current=start;
        int i=0;
        while( current->next!=NULL ) {
            printf("node%d= %s\n",++i,current->str);
            current=current->next;

        }
        printf("this the end");
        }


    int main() {

        char c;
        int input;

        do {
            printf("Select from the following options:\n"
               "1.Display list\n"
               "2.Add to list\n"
               "3.delete from list\n"
               );


        scanf("%d",&input);
        switch (input) {

            case 1: display(); break;
            case 2: insert(); break;
        //    case 3: delete(); break;
            default : printf("Please select 1 , 2 or 3\n");

            }
        printf( "would you like to continue?(y/n)\n");
        scanf("%s",&c);
        }while(c=='y');

    return 0;
    }
3
  • But you are also using n->str=malloc(sizeof(char) * 1000); in your working code !!
    – hunterr986
    Aug 19, 2012 at 23:18
  • Will this work while accessing a linked list ? ... (start->next)->next .... I know it does not work but is there a way to do this without the use of an other structure ?
    – hunterr986
    Aug 19, 2012 at 23:20
  • @hunterr986:- why dont you put if( start==NULL ) { start=n; current=start; } :)
    – perilbrain
    Aug 19, 2012 at 23:25
1
if( start==NULL ) {
        start->next=n;
        current=n;
    }

I think you have a little problem here ;-)

2
  • hmmm .... i assigned the structure to NULL in the beginning of the code. so that check should now do anything right ? Please correct me here.. Thanks
    – hunterr986
    Aug 19, 2012 at 23:09
  • @hunterr986 problem is accessing start->next when start is NULL Aug 19, 2012 at 23:16
1

the next line is not safe:

   scanf("%s",&c);

scanf adds terminating null character '\0' that corrupts stack in your case.

1
  • Thanks for letting me know ! but whenever i used getchar() the program was terminating immediately whenever it reached that line in code. so i used scanf to make it work !
    – hunterr986
    Aug 19, 2012 at 23:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.