Author Topic: error: conflicting types  (Read 2067 times)

0 Members and 1 Guest are viewing this topic.

Offline arulTopic starter

  • Regular Contributor
  • *
  • Posts: 50
  • Country: in
error: conflicting types
« on: November 16, 2018, 03:27:08 pm »
Hi,

I create a one function.
And called that function in main function like function_name(variable). I got some error like as "error: conflicting types". But when i called that function as return_type function_name(data_type variable) , i got no error.

Please let me know anyone.
 

Offline sokoloff

  • Super Contributor
  • ***
  • Posts: 1799
  • Country: us
Re: error: conflicting types
« Reply #1 on: November 16, 2018, 03:32:49 pm »
Add a line before main:

void UART_initialize();  // Or whatever the function name was; I'm not going back to extract it from the *image* you posted

to declare (but not define) the function.
 
The following users thanked this post: arul

Offline sokoloff

  • Super Contributor
  • ***
  • Posts: 1799
  • Country: us
Re: error: conflicting types
« Reply #2 on: November 16, 2018, 03:33:43 pm »
Or put the main function at the bottom of the file rather than the top. That's a fairly typical pattern as well.
 
The following users thanked this post: arul

Offline mfro

  • Regular Contributor
  • *
  • Posts: 210
  • Country: de
Re: error: conflicting types
« Reply #3 on: November 16, 2018, 03:40:32 pm »
Hi,

I create a one function.
And called that function in main function like function_name(variable). I got some error like as "error: conflicting types".
Actually, you got a warning *and* an error. Which both should have told you what you have done wrong.

Your main() (where you try to call UART_initialize()) is seen by the compiler *before* the called function which means that the called function and its return type is unknown at that place (you got a warning regarding that). C can deal with that, but will assume that the function will return an int by default. Later on, the compiler found the real function declaration, but you declared a void return type which generated the error regarding conflicting types.

Either locate your UART_initialize() function before main() or add a function prototype there.

Quote
But when i called that function as return_type function_name(data_type variable) , i got no error.
Can you rephrase that? I don't get it.
Beethoven wrote his first symphony in C.
 
The following users thanked this post: arul

Offline sokoloff

  • Super Contributor
  • ***
  • Posts: 1799
  • Country: us
Re: error: conflicting types
« Reply #4 on: November 16, 2018, 03:48:04 pm »
Agreeing with and expanding on mfro's answer: I recommend to developers that they enable whatever setting on their platform is "treat warnings as errors" and get into the habit of writing code that compiles without warnings.

There are a few spurious warnings for sure, but in general, you'll save time and have more predictable progress if you treat warnings as errors and learn how to let the compiler know "yeah, don't worry; I got this" on the occasions you need to.
 
The following users thanked this post: arul

Offline rstofer

  • Super Contributor
  • ***
  • Posts: 9889
  • Country: us
Re: error: conflicting types
« Reply #5 on: November 16, 2018, 03:52:21 pm »
You need to READ the error messages, carefully...

The first one (line 12), a warning about an implicit declaration means that, at the time the compiler encountered the line of code, the function had not been declared.  OK, it isn't declared and it needs to be.  Where is my declaration?  Why isn't it declared before the compiler encountered the line of code?  Scanning the file from top to bottom, I can see that my function hasn't been defined before it is used.  Hm...  Maybe if I move the function to the top?  Maybe I should read up on function prototypes (but that takes a little more experience).  BTW, this is a common problem for new C programmers.  My function is right there!  It's in the file, right there!  But it needs to be declared BEFORE it is called.  Separate files require .h files to provide prototypes.  That's coming up next!

Anyway, the compiler created a dummy declaration that had no return type.  Just something to fill in the symbol table.

Then when the compiler encountered the declaration returning 'void' (line 30), there was a conflict in return types.

Modern compilers do give pretty good diagnostics.


« Last Edit: November 16, 2018, 05:22:52 pm by rstofer »
 
The following users thanked this post: arul

Offline sokoloff

  • Super Contributor
  • ***
  • Posts: 1799
  • Country: us
Re: error: conflicting types
« Reply #6 on: November 16, 2018, 03:59:24 pm »
Anyway, the compiler created a dummy declaration that had no return type.  Just something to fill in the symbol table.

Then when the compiler encountered the declaration returning 'void' (line 30), there was a conflict in return types.
Minor nit: the compiler created a dummy declaration with return type of int. (Doesn't change the message at all, which is spot-on.)
 
The following users thanked this post: arul

Offline rstofer

  • Super Contributor
  • ***
  • Posts: 9889
  • Country: us
Re: error: conflicting types
« Reply #7 on: November 16, 2018, 05:40:44 pm »
Anyway, the compiler created a dummy declaration that had no return type.  Just something to fill in the symbol table.

Then when the compiler encountered the declaration returning 'void' (line 30), there was a conflict in return types.
Minor nit: the compiler created a dummy declaration with return type of int. (Doesn't change the message at all, which is spot-on.)

Yes, it does!

In the grand old days, we talked about 'top down' programming.  It makes every bit of sense to put main() at the top of the file because it is the 'top' in 'top down'.  By extension, lesser functions would be further down until you reached the lowest level function at the bottom of the file (assuming all code in one big file).

It may make sense but that's not the way it works in C.  We can sort of emulate the 'top down' style by using function prototypes above main() while leaving the actual code in some lower position in the file.

This is a fairly decent description:
https://en.wikipedia.org/wiki/Function_prototype

 
The following users thanked this post: arul

Offline arulTopic starter

  • Regular Contributor
  • *
  • Posts: 50
  • Country: in
Re: error: conflicting types
« Reply #8 on: November 17, 2018, 05:58:59 am »
Thanks to all.
Before that main function, i put my user defined function.
After am successfully compiled.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf