tags:

views:

661

answers:

6

Hello,

I'm using the below code:

char dest[5];
char src[5] = "test";

printf("String: %s\n", do_something(dest, src));

char *do_something(char *dest, const char *src)
{
return dest;
}

The implementation of do_something is not important here. When I try to compile the above I get these two exception:

"error: conflicting types for 'do_something'" (at the printf call) "error: previous implicit declaration of 'do_something' was here" (at the prototype line)

Why?

+1  A: 

You didn't declare it before you used it.

You need something like

char *do_something(char *, const char *);

before the printf.

Example:

#include <stdio.h>
char *do_something(char *, const char *);
char dest[5];
char src[5] = "test";
int main ()
{
printf("String: %s\n", do_something(dest, src));
 return 0;
}

char *do_something(char *dest, const char *src)
{
return dest;
}

Alternatively, you can put the whole do_something function before the printf.

Kinopiko
+6  A: 

You are trying to call do_something before you declare it. You need to add a function prototype before your printf line:

char* do_something(char*, const char*);

Or you need to move the function definition above the printf line. You can't use a function before it is declared.

James McNellis
ohhh, such a lame mistake, thanks
goe
Everybody makes lame mistakes on occasion...
James McNellis
+2  A: 

You have to declare the function before you use it. If the function name appears before its declaration, C compiler will follow certain rules and makes the declaration itself. If it is wrong, you will get that error.

You have two options: (1) define it before you use it, or (2) use forward declaration without implementation. For example:

char *do_something(char *dest, const char *src);

Note the semicolon at the end.

Viliam
+1  A: 

When you don't give a prototype for the function before using it, C assumes that it takes any number of parameters and returns an int. So when you first try to use do_something, that's the type of function the compiler is looking for. Doing this should produce a warning about an "implicit function declaration".

So in your case, when you actually do declare the function later on, C doesn't allow function overloading, so it gets pissy because to it you've declared two functions with different prototypes but with the same name.

Short answer: declare the function before trying to use it.

mrduclaw
No, it assumes it takes any number of parameters and returns an int. In C, there's a big difference between `int foo()` (takes any number of parameters) and `int foo(void)` (takes no parameters).
Adam Rosenfield
Thanks for the clarification! Updated :)
mrduclaw
+2  A: 

When you call an undeclared function, C assumes that it returns an 'int' and also attempts to derive the types of its parameters from the types of the actual arguments (no, it doesn't assume that it has no parameters, as someone suggested before).

In your specific example such an implictly derived declaration for 'do_something' would look as follows

int do_something(char *, char *)

However, later in the code you declare 'do_something' as

char *do_something(char *, const char *)

As you can see, these declarations are different from each other (because of the return type, the 'const' bit is not important here). This is what the compiler doesn't like.

AndreyT
A: 

C Commandment #3:

K&R #3 Thou shalt always prototype your functions or else the C compiler will extract vengence.

http://www.ee.ryerson.ca:8080/~elf/hack/God.vs.K+R.html

brtc