D
dentzigui
hi, i have being trying study k&r book. and i am puzzled with this:
#include <stdio.h>
char *strdup(char *);
int main(void)
{
char *word = "this is a test";
char *p;
p = strdup(word);
printf("%s\n", p);
return 1;
}
char *strdup(char *s)
{
char *p;
p = (char *) malloc(strlen(s) + 1);
if (p != NULL)
strcpy(p, s);
return p;
}
when compiled i have:
teste.c: In function ‘strdup’:
teste.c:19: warning: implicit declaration of function ‘malloc’
teste.c:19: warning: incompatible implicit declaration of built-in
function ‘malloc’
teste.c:19: warning: implicit declaration of function ‘strlen’
teste.c:19: warning: incompatible implicit declaration of built-in
function ‘strlen’
teste.c:21: warning: implicit declaration of function ‘strcpy’
teste.c:21: warning: incompatible implicit declaration of built-in
function ‘strcpy’
and when the same code but with:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
.....
i have:
teste.c:5: error: conflicting types for ‘strdup’
teste.c:18: error: conflicting types for ‘strdup’
could someone help me understand/learn?
#include <stdio.h>
char *strdup(char *);
int main(void)
{
char *word = "this is a test";
char *p;
p = strdup(word);
printf("%s\n", p);
return 1;
}
char *strdup(char *s)
{
char *p;
p = (char *) malloc(strlen(s) + 1);
if (p != NULL)
strcpy(p, s);
return p;
}
when compiled i have:
teste.c: In function ‘strdup’:
teste.c:19: warning: implicit declaration of function ‘malloc’
teste.c:19: warning: incompatible implicit declaration of built-in
function ‘malloc’
teste.c:19: warning: implicit declaration of function ‘strlen’
teste.c:19: warning: incompatible implicit declaration of built-in
function ‘strlen’
teste.c:21: warning: implicit declaration of function ‘strcpy’
teste.c:21: warning: incompatible implicit declaration of built-in
function ‘strcpy’
and when the same code but with:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
.....
i have:
teste.c:5: error: conflicting types for ‘strdup’
teste.c:18: error: conflicting types for ‘strdup’
could someone help me understand/learn?