问题
In K&R Chapter 1.9, I've been experimenting with the program provided below. Particularly, what would happen if I removed certain decelerations of functions.
So, I removed line #4. int getline(char line[], int maxline
And the program complies perfectly and functions properly as far as I'm aware.
When I remove line #5. void copy(char to[], char from[]);
The program throws the following error:
yay.c:37:6: warning: conflicting types for ‘copy’ void copy(char to[], char from[])
yay.c:15:9: note: previous implicit declaration of ‘copy’ was here copy(longest, line);
#include <stdio.h>
#define MAXLINE 1000
int getline(char line[], int maxline);
void copy(char to[], char from[]);
main()
{
int len;
int max;
char line[MAXLINE];
char longest[MAXLINE];
max = 0;
while ((len = getfatline(line, MAXLINE)) > 0)
if (len > max) {
max = len;
copy(longest, line);
}
if (max > 0)
printf("%s", longest);
return 0;
}
int getfatline(char s[], int lim)
{
int c, i;
for (i=0; i<lim-1 && (c=getchar()) !=EOF && c != '\n'; ++i)
s[i] = c;
if (c == '\n') {
s[i] = c;
++i;
}
s[i] = '\0';
return i;
}
void copy(char to[], char from[])
{
int i;
i = 0;
while ((to[i] = from[i]) != '\0')
++i;
}
Could anyone explain this to me?
回答1:
As per the latest C standard C11
, every function has to be (at least) declared before it has been used. In that way, compiler will have the knowledge of the function signature.
In the code, while calling a function, if the declaration (at least) is not visible to the compiler, (due to some legacy reason), the compiler (used to) assume
- The function returns an
int
- accepts any number of paramater.
Later, when you define the function to have a return type other than int
, it will create the conflict.
That is why,
- removing the forward declaration of
getline()
produces no error. - removing the forward declaration of
copy()
produces the error, mismatch in the return type.
Along the same line, main()
is no longer required to be supported, as implicit int
has also been removed. You should write int main(void)
, at least to be standard conforming.
回答2:
To add a function you need to give a prototype for it. Thats what this is
void copy(char to[], char from[]);
But if you don't provide the prototype, the return type of the function is taken as int
by default.
This is the reason why it is working for the function getfatline()
, because its return type is int
when you are using it later in the code, which is the same as taken by default when you remove the prototype.
But when you remove the prototype for the function copy
, the return type is set to int
by default but your function definition has the return type as void
, that is where the conflict is occurring, and it is throwing an error.
回答3:
Getline is actually defined in in the stdio.h
header
ssize_t getline(char **lineptr, size_t *n, FILE *stream);
hence deleting your own getline implementation will not throw an error. Instead, deleting your other function, will throw an error because no such function does exist in stdio.h but it's user-defined. By the way, in this case the problem you are facing can be simply resolved by deleting the function itself from the program.
来源:https://stackoverflow.com/questions/34880787/why-is-a-conflicting-type-error-being-thrown-when-i-execute-this-program