Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
241 views
in Technique[技术] by (71.8m points)

c - Program to sort list in alphabetical order crashes

I wrote this program that writes some data on a .txt and then reads it and create a list by sorting in alphabetical order. The program seems to work but soon after I see the output it crashes. Could you help me understand why?

I get this error:

warning: assignment to 'struct node *' from incompatible pointer type 'list' {aka 'struct nodeList *'} [-Wincompatible-pointer-types]|

Proably this is a very basic issue about pointers but can't understand where's the problem. Any advice about the function sort is welcome. Thanks in advance.

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

typedef struct {
    char name[20];
    int time;
}elem;

typedef struct nodeList{
    elem el;
    struct node *next;
}node;

typedef node *list;


bool isless(elem e1, elem e2){
    return (strcmp(e1.name, e2.name)<0);
}

void sort(list *l, elem e){
    list t;
    list prev;
    list cursor;
    t=(node*)malloc(sizeof(node));
    if(t==NULL){
        printf("Error malloc
");
        system("pause");
        exit(1);
    }
    t->el=e;

    if(*l==NULL){
        *l=t;
    }
    else{
        prev=NULL;
        cursor=*l;
        while(cursor!=NULL){
            if(!isless(cursor->el,t->el)){
                if(prev==NULL){
                    *l=t;
                    t->next=cursor;
                }
                else{
                    prev->next=t;
                    t->next=cursor;
                }
                break;
            }
            prev=cursor;
            cursor=cursor->next;
        }
        if(cursor==NULL){
            prev->next=t;
            t->next=cursor;
        }
    }
}

void stamp(list l){
    if(l==NULL){
        printf("Empty list
");
    }
    while(l!=NULL){
        printf("%s%d
",(l->el).name,(l->el).time);
        l=l->next;
    }
}

int main()
{
    elem e;
    int T;
    char N[20];
    FILE *f1;
    list head=NULL;

    f1=fopen("STORAGE.TXT","w");

    //user input
    while(1){
        printf("Insert a name (or write stop):");
        scanf("%s",N);
        if(strcmp(N,"stop")==0)
            break;
        printf("Insert a time:");
        scanf("%d",&T);
        strcpy(e.name,N);
        e.time=T;
        fwrite(&e,sizeof(elem),1,f1);
    }
    fclose(f1);

    //create and sort a list in alphabetical order
    f1=fopen("STORAGE.TXT","r");
    while(fread(&e,sizeof(elem),1,f1)>0)
        sort(&head,e);
    fclose(f1);

    //stamp list
    stamp(head);

    return 0;
}

question from:https://stackoverflow.com/questions/65919101/program-to-sort-list-in-alphabetical-order-crashes

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Best is to run the program with valgrind, or something like that. This will show you where your program crashes. If you export debug symbols, it will even tell you exactly at which line of code! However, I do see a couple of problems with your code:

  • your elem-struct has a char[20]. You don't check for the length of the input, this might crash for longer strings. Use strncpy instead of strcpy, of check the length before copying.

  • You don't initialize pointers. Bad practice! I guess that is why your program crashes. Change:

    list t = NULL; list prev = NULL; list cursor = NULL;

And also, right after your malloc:

t.next = NULL;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...