从类型'char *'分配类型'char [50]'时不兼容的类型

时间:2015-06-03 20:22:35

标签: c list hyperlink crash

我正在尝试创建一个链接列表。我有一个功能,从我的链接列表中删除(删除功能)的东西。但是当我尝试比较字符串时它似乎崩溃了。它一直工作到最后一个随机的printf语句。

以下是代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node {
    char data[50];
    struct node *next;
}*head;

int main()
{
    //creates a sudo user interface
    printf("1. Insert\n");
    printf("2. Display\n");
    printf("3  Count\n");
    printf("4. Delete\n");
    printf("5. Exit\n");

    //create the portion that decides the user input
    int userSelection = 0 ;
    scanf("%d", &userSelection);
    userSelect(userSelection);


    return 0;
}
//this function will handle user imput
void userSelect(int num)
{
    if(num == 1)
    {
        printf("What is your name?\n");
        char userName[30];
        scanf(" %s", userName);
        add(userName);

    } else if(num ==2) {
        display();

    } else if (num ==2){
        //do something
    } else if (num ==3){
        printf("%d", count());
        main();
    } else if (num ==4 ){
        char delName[50];
        printf("Who do you want to remove?\n");
        scanf("%s", delName);
        delete(delName);
    }else if (num == 5){
        // return 0;
    }else if (num > 6){
        printf("\n52Please make sure you use a valid option!\n\n");
        main();
    }


}

void add( char userName[] )
{
    struct node *temp;

    temp=(struct node *)malloc(sizeof(struct node));

    strcpy(temp->data, userName);

    if (head== NULL)
    {
        head=temp;
        head->next=NULL;
    }
    else
    {
        temp->next=head;
        head=temp;
    }
    printf("\n");
    main();
}
int count()
{
    struct node *n;
    int c=0;
    n=head;
    while(n!=NULL)
    {
        n=n->next;
        c++;
    }
    printf("\n");
    return c;
}
void display(struct node *r)
{
    r=head;
    if(r==NULL)
    {
        return;
    }
    while(r!=NULL)
    {
        printf("%s ",r->data);
        r=r->next;
    }
    printf("\n");
    main();
}


void delete(char delName)
{
    struct node *temp, *prev;
    temp=head;
    printf("sffs\n");
    while(temp!=NULL)
    {
        printf("sdg\n");
        if(strcmp(temp->data, delName)==0)
        {
            if(temp==head)
            {
                head=temp->next;
                free(temp);
                return 1;
            }
            else
            {
                prev->next=temp->next;
                free(temp);
                return 1;
            }
        }
        else
        {
            prev=temp;
            temp= temp->next;
        }
    }
    main();
}

我认为它与删除功能中的strcmp有关。但是,有没有人有任何想法?

1 个答案:

答案 0 :(得分:1)

主要问题是:

void delete(char delName)

应该是这样的:

void delete(char *delName)

但是,您的程序还有许多其他问题,需要很长时间才能列出所有这些问题。我只会给你两件事要解决:

  1. 您应该打开警告并修复所有警告。

  2. 您的功能不应该致电main()。相反,main()应该包含一个循环,如下所示:

    //create the portion that decides the user input
    int userSelection = 0 ;
    do {
        scanf("%d", &userSelection);
        userSelect(userSelection);
    } while (userSelection != 5);