0

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

0

2 Answers 2

9

You may need to add

#include <string.h>

to get the declaration of strcpy().

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.