C 中的链表

发布于 2024-11-05 10:38:27 字数 1065 浏览 0 评论 0原文

当我编译这个程序时,我在第 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

只想待在家 2024-11-12 10:38:27

您可能需要添加

#include <string.h>

以获取 strcpy() 的声明。

You may need to add

#include <string.h>

to get the declaration of strcpy().

浮华 2024-11-12 10:38:27

不是因为这比安德鲁的答案更好,而是因为 gcc 给我的关于您的代码的所有警告都不太适合注释。

/usr/bin/gcc    -c -o str.o str.c
str.c: In function 'insert':
str.c:53: warning: format '%c' expects type 'char *', but argument 2 has type 'char (*)[30]'
str.c:57: warning: incompatible implicit declaration of built-in function 'strcpy'
str.c: In function 'print':
str.c:79: warning: format '%c' expects type 'int', but argument 2 has type 'char *'

gcc 一定给了你“隐式声明”的警告,不要忽视这样的事情。更好的是,使用 c99 和选项 -Wall 来获取更多警告,然后将其全部纠正。

c99 -Wall   -c -o str.o str.c
str.c: In function 'main':
str.c:30: warning: unused variable 'p'
str.c: In function 'insert':
str.c:52: warning: format '%c' expects type 'char *', but argument 2 has type 'char (*)[30]'
str.c:56: warning: implicit declaration of function 'strcpy'
str.c:56: warning: incompatible implicit declaration of built-in function 'strcpy'
str.c:49: warning: unused variable 'junk'
str.c:48: warning: unused variable 'num'
str.c: In function 'print':
str.c:78: warning: format '%c' expects type 'int', but argument 2 has type 'char *'
str.c:81: warning: 'return' with no value, in function returning non-void

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.

/usr/bin/gcc    -c -o str.o str.c
str.c: In function 'insert':
str.c:53: warning: format '%c' expects type 'char *', but argument 2 has type 'char (*)[30]'
str.c:57: warning: incompatible implicit declaration of built-in function 'strcpy'
str.c: In function 'print':
str.c:79: warning: format '%c' expects type 'int', but argument 2 has type 'char *'

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.

c99 -Wall   -c -o str.o str.c
str.c: In function 'main':
str.c:30: warning: unused variable 'p'
str.c: In function 'insert':
str.c:52: warning: format '%c' expects type 'char *', but argument 2 has type 'char (*)[30]'
str.c:56: warning: implicit declaration of function 'strcpy'
str.c:56: warning: incompatible implicit declaration of built-in function 'strcpy'
str.c:49: warning: unused variable 'junk'
str.c:48: warning: unused variable 'num'
str.c: In function 'print':
str.c:78: warning: format '%c' expects type 'int', but argument 2 has type 'char *'
str.c:81: warning: 'return' with no value, in function returning non-void
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文