简体   繁体   中英

Conflicting types with char*

I have a small program to test passing char* pointers in and out of functions. When I compile with cc, I get warning and errors saying I have conflicting types even though all my variables are char* . Please enlighten

#include <stdio.h>

main()
{
    char* p = NULL;

    foo1(p);
    foo2();
}

void foo1(char* p1)
{
}

char* foo2(void)
{
    char* p2 = NULL;

    return p2;
}

p.c:11: warning: conflicting types for ‘foo1’
p.c:7: warning: previous implicit declaration of ‘foo1’ was here
p.c:15: error: conflicting types for ‘foo2’
p.c:8: error: previous implicit declaration of ‘foo2’ was here

You need to prototype your functions before the main() function.

example:

void foo1(char *p1);
char* foo2(void);

int main(.......

Or just put the bodies for those functions above the main function.

As ghills said, to fix the error, move the function definitions above main() or put function prototypes there.

The reason for the error is that when the compiler sees:

foo1(p);
foo2();

before it sees either a declaration or definition of foo1() and foo2() , it assumes the return type of those functions is int . In the early days of C, int was considered to be a reasonable default return type (there was no void type in the earliest versions of C). Nowadays, omitting the return type is considered bad practice and compilers complain about it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM