Conflicting types with char *

I have a little program for testing passing char * pointers in and out of functions. When I compile with cc I get a 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

      

+1


a source to share


2 answers


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 these functions above the main function.

+16


a source


As ghills said, to fix the bug, move 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 the declaration or definition foo1()

and foo2()

, it assumes that the return type of these functions is int

. In the early days of C, it int

was considered a reasonable default return type (there was no type in earlier versions of C void

). It is currently considered bad practice to exclude a return type, and compilers complain about it.

+3


a source







All Articles