从类型 'char *' 分配给类型 'char[50]' 时类型不兼容
incompatible types when assigning to type 'char[50]' from type 'char *'
我正在尝试创建一个 link 列表。我有一个功能可以从我的 link 列表中删除(删除功能)内容。但是当我尝试比较字符串时它似乎崩溃了。它一直工作到最后一个随机 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 有关..但是有人有任何想法吗?
主要问题在于:
void delete(char delName)
应该是这样的:
void delete(char *delName)
但是,您的程序还有很多其他问题,要列出所有问题需要很长时间。我只会给你两件事来解决:
您应该打开警告并修复所有警告。
您的函数不应调用 main()
。相反,main()
应该包含一个循环,如下所示:
//create the portion that decides the user input
int userSelection = 0 ;
do {
scanf("%d", &userSelection);
userSelect(userSelection);
} while (userSelection != 5);
我正在尝试创建一个 link 列表。我有一个功能可以从我的 link 列表中删除(删除功能)内容。但是当我尝试比较字符串时它似乎崩溃了。它一直工作到最后一个随机 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 有关..但是有人有任何想法吗?
主要问题在于:
void delete(char delName)
应该是这样的:
void delete(char *delName)
但是,您的程序还有很多其他问题,要列出所有问题需要很长时间。我只会给你两件事来解决:
您应该打开警告并修复所有警告。
您的函数不应调用
main()
。相反,main()
应该包含一个循环,如下所示://create the portion that decides the user input int userSelection = 0 ; do { scanf("%d", &userSelection); userSelect(userSelection); } while (userSelection != 5);