C 中的链表
当我编译这个程序时,我在第 45 行(已注释)中收到一个错误,指出 strcpy 的隐式声明不兼容...我复制了该代码的一部分,希望你们可以帮助我
#include <stdio.h>
#include <stdlib.h>
#define strsize 30
typedef struct member
{int number;
char fname[strsize];
struct member *next;
}
RECORD;
RECORD* insert (RECORD *it);
RECORD* print(RECORD *it, int j);
int main (void)
{
int i, result;
RECORD *head, *p;
head=NULL;
printf("Enter the number of characters: ");
scanf("%d", &result);
for (i=1; i<=result; i++)
head=insert (head);
print (head, result);
return 0;
}
RECORD* insert (RECORD *it)
{
RECORD *cur, *q;
int num;
char junk;
char first[strsize];
printf("Enter a character:");
scanf("%c", &first);
cur=(RECORD *) malloc(sizeof(RECORD));
strcpy(cur->fname, first);
cur->next=NULL;
if (it==NULL)
it=cur;
else
{
q=it;
while (q->next!=NULL)
q=q->next;
q->next=cur;
}
return (it);
}
RECORD* print(RECORD *it, int j)
{
RECORD *cur;
cur=it;
int i;
for(i=1;i<=j;i++)
{
printf("%c \n", cur->fname);
cur=cur->next;
}
return;
}
使用 GCC 来解决这个问题
When I compile this program I get an error in line 45 (commented) saying incompatible implicit declaration of strcpy...I copied part of that code and hopefully you guys can help me figure this out
#include <stdio.h>
#include <stdlib.h>
#define strsize 30
typedef struct member
{int number;
char fname[strsize];
struct member *next;
}
RECORD;
RECORD* insert (RECORD *it);
RECORD* print(RECORD *it, int j);
int main (void)
{
int i, result;
RECORD *head, *p;
head=NULL;
printf("Enter the number of characters: ");
scanf("%d", &result);
for (i=1; i<=result; i++)
head=insert (head);
print (head, result);
return 0;
}
RECORD* insert (RECORD *it)
{
RECORD *cur, *q;
int num;
char junk;
char first[strsize];
printf("Enter a character:");
scanf("%c", &first);
cur=(RECORD *) malloc(sizeof(RECORD));
strcpy(cur->fname, first);
cur->next=NULL;
if (it==NULL)
it=cur;
else
{
q=it;
while (q->next!=NULL)
q=q->next;
q->next=cur;
}
return (it);
}
RECORD* print(RECORD *it, int j)
{
RECORD *cur;
cur=it;
int i;
for(i=1;i<=j;i++)
{
printf("%c \n", cur->fname);
cur=cur->next;
}
return;
}
using GCC to complie
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能需要添加
以获取 strcpy() 的声明。
You may need to add
to get the declaration of strcpy().
不是因为这比安德鲁的答案更好,而是因为 gcc 给我的关于您的代码的所有警告都不太适合注释。
gcc
一定给了你“隐式声明”的警告,不要忽视这样的事情。更好的是,使用 c99 和选项 -Wall 来获取更多警告,然后将其全部纠正。Not because this would be better than Andrew's answer but because all the warnings that
gcc
gives me on your code don't fit well into a comment.gcc
must have given you the warning of "implicit declaration", don't ignore such things. Even better, use c99 and the option -Wall to get more warnings, and then correct them all.