当我编译这个程序时,我在第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编译
答案 0 :(得分:9)
您可能需要添加
#include <string.h>
获取strcpy()的声明。
答案 1 :(得分:0)
不是因为这会比安德鲁的回答更好,而是因为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