1

I have been working on a function searchn() which takes in a linked list list and a string lname.

It compiles perfectly fine, but I get a segmentation fault (core dumped) when I try running the function. I ran it through Valgrind and it tells me that I'm using strcpy and strcmp incorrectly. My struct for player has also been included for reference. Can anybody see what I'm doing wrong? Sorry, I'm not the most proficient in coding.

Any help would be great. Thanks.

struct player {
    char* fname;
    char* lname;
    char pos;
    int val;
    int rank;
    struct player* next;
};

void searchn(struct player* list, char* lname){

    while (list!=NULL && strcmp(list->lname, lname) != 0){
        list = list->next;
    }

    if (list != NULL && strcmp(list->lname, lname)==0) {
        printf("%s found! \n", lname);
        printf("%s \n", list->lname);
        printf("%s \n", list->fname);
        printf("%c \n", list->pos);
        printf("%d \n", list->val);
        printf("\n");
    }
}

The following is how the method that populates the linked list.

void addp (struct player* newnode, struct player* list){
    struct player* templist1;
    // if the list is non empty.
    if (list !=NULL){
        if(newnode->pos == GOALKEEPER){  //insert if G.
            while (list->next != NULL && (list->next)->rank < 1){
                list = list->next;
            }
            templist1 = list->next;
            list->next = newnode;
            newnode->next = templist1;
        }
        if(newnode->pos == DEFENDER){// after G bef M.
            // iterate through templist.
            while (list->next != NULL && (list->next)->rank < 2) {  // go to end of G.
                // when the list isn't empty next node rank is less than one, keep going
                list = list -> next;
            }
            // when finally rank == or > 1, then add newnode.
            templist1 = list->next;
            list->next = newnode;
            newnode->next = templist1;
        }
        if(newnode->pos == MIDFIELDER){ //after G and M but before S
            while (list->next != NULL && (list->next)->rank < 3) {
                list = list -> next;
            }
            // when stopped, then add newnode.
            templist1 = list->next;
            list->next = newnode;
            newnode->next = templist1;
        }
        if(newnode->pos == STRIKER){ // at the end.
            while (list->next != NULL && (list->next)->rank < 4){
                list = list -> next;
            }
            templist1 = list->next;
            list->next = newnode;
            newnode->next = templist1;
        }
        printf("player added");
    }
}
4

2 回答 2

2

您的while循环不断比较相同的两个字符串,因为它不会将列表元素中的新字符串加载到 temp 中。无论如何,您实际上并不需要 temp 变量。

在您当前的逻辑中,您可以将 just 移动strcpywhile循环内:

while (list!=NULL && strcmp(temp, lname) != 0){
    strcpy(temp, list->lname);
    list = list->next;
}

但是你可以完全摆脱温度。改变这个:

strcpy(temp, list->lname);

while (list != NULL && strcmp(temp, lname) != 0) {
    list = list->next;
}

if (strcmp(temp, lname) == 0) {

对此:

while (list != NULL && strcmp(list->lname, lname) != 0) {
    list = list->next;
}

if (list != NULL) {
于 2013-07-21T00:09:10.097 回答
1

正如它所回答的那样,您不会更新 temp。你也不需要它。此外,如果字符串超过 1024 字节,您可能会遇到漏洞。尝试这个:

while (list != NULL && strcmp(list->lname, lname) != 0) {
    list = list->next;
}

if (list != NULL) {

最后一个条件中的 strcmp 是不必要的。

于 2013-07-21T00:19:25.120 回答