gpt4 book ai didi

c - 在函数中使用 strcpy

转载 作者:太空宇宙 更新时间:2023-11-04 05:43:21 25 4
gpt4 key购买 nike

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

struct person *create_node(char *, char *,int);
void addnode(char *,char *,int,struct person *);


struct person
{
char fname[20];
char sname[20];
int pno;
struct person *next,*prev;
};

struct person *create_node(char *first,char *second,int mob)
{
struct person *pnode=(struct person *)malloc(sizeof(struct person));
strcpy(pnode->fname,*first);
strcpy(pnode->sname,*second);
pnode->pno=mob;
pnode->next=pnode->prev=NULL;

return pnode;

}

void addnode(char *first,char *second,int mob,struct person *pnode)
{
while(pnode->next!=NULL)
{
pnode=pnode->next;
}
pnode->next=create_node(first,second,mob);
pnode->next->prev=pnode;

}

int main()
{
struct person *root=NULL;
char choice='y';
char first[20];
char second[20];
int mob;

while(tolower(choice)=='y')
{
printf("enter the first name:");
scanf("%s",first);

printf("enter the second name:");
scanf("%s",second);

printf("enter the mobile no:");
scanf("%d",&mob);

if(root==NULL)
root=create_node(first,second,mob);
else
addnode(first,second,mob,root);
printf("enter the option to continue or end(y or n):");
scanf("%c",&choice);
fflush(stdin);
}

return 0;
}

这是我写的程序,它基本上做的是,它将创建从用户那里获取结构值的链表。

当我从函数运行这个程序时,我得到了 2 个类似的警告

    struct person * create_node(char *, char *, int),
passing char to argument 2 of strcpy(char *, const char *) lacks a cast

我不明白为什么要将 const 值传递给函数。

这个程序还有一个问题。在我输入第一个节点的信息后程序停止工作。

我在 windows 平台上使用 gcc 编译器。请帮帮我。谢谢...

最佳答案

问题不在于此。问题是您将 *first*second 作为 strcpy() 的第二个参数传递给 chars 而不是 char 指针。您必须简单地传递 firstsecond,即指针本身。

另请do not cast the return value of malloc.

关于c - 在函数中使用 strcpy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12549952/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com