GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 25-Jul-2007, 05:32
1978929 1978929 is offline
New Member
 
Join Date: Jul 2007
Posts: 5
1978929 is on a distinguished road

Conflicting types


In my code i only call method in main.but it shows conflicting types for 'function name' error.why this is occure?
  #2  
Old 25-Jul-2007, 06:11
davis
 
Posts: n/a

Re: conflicting types


Quote:
Originally Posted by 1978929
In my code i only call method in main.but it shows conflicting types for 'function name' error.why this is occure?

Whatever you do, don't show the offending code. That might be too helpful for those trying to assist you.

The likelihood is that you are invoking a function using the wrong type(s) as (a) parameter(s) to the function.


:davis:
  #3  
Old 25-Jul-2007, 09:31
Howard_L Howard_L is offline
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 470
Howard_L has a spectacular aura aboutHoward_L has a spectacular aura about

Re: conflicting types


What davis means is:
1. You did not post any code for us to be able to see what you were doing wrongly.
2. When using a function declaration in the head area with the actual function following main() you do not need to include the 'data names'... BUT the 'data TYPES' must MATCH EXACTLY.
CPP / C++ / C Code:
#include <stdio.h>
   
int func1(int, int, char, unsigned char);           /* function declaration */

int main(void) {
  ...stuff...; return 0;
}

int func1(int x, int y, char c, unsigned char d) {  /* actual function      */
  ...stuff...; return 0;
}
Some people actually include all the other functions before main() in which case no declarations are needed because they declare themselves and they are before main.
Howard;
  #4  
Old 25-Jul-2007, 21:53
1978929 1978929 is offline
New Member
 
Join Date: Jul 2007
Posts: 5
1978929 is on a distinguished road

Re: conflicting types


I have attached my code here.Can u pls help me ?why those conflicting error come?
CPP / C++ / C Code:
#include <stdio.h>
#include <string.h>
 

void index(int argc,char *argv[])
{
    int i;
    int j;
    int k;
    int ptr;
    int l;
    k = 0;
    for (i = 2; i < argc; i++) {
if (argv[i]!=NULL)
{
              for (j = 1; j < i; j++) {
             ptr = strcmp(argv[j], argv[i]);
            if (ptr == 0) {
                for (l = i; l < argc; l++) {
                    argv[l] = argv[l + 1];
                }
                i--;
		printf("%d\n", j - 1);
		j--;
               break; /* break out of the for(j = 1; j < i...) loop */
            }
        }
        if (j == i)  
     		{
	      k++;
            printf("%d\n", k);
                  }
        	} 
 	}
}
main(int argc, char *argv[])
{
   
int b;
b=0;
    
index(argc, argv);

}

Last edited by LuciWiz : 26-Jul-2007 at 13:23. Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
  #5  
Old 25-Jul-2007, 22:47
Howard_L Howard_L is offline
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 470
Howard_L has a spectacular aura aboutHoward_L has a spectacular aura about

Re: conflicting types


Hmmm, in mingw I get the warning:
Code:
gcc -Wall -W -pedantic conflictypes_x1.c conflictypes_x1.c:4: warning: conflicting types for built-in function `index'
...so it would seem thet there is a 'built-in' function by that name already.
I'm not familiar with it. I searched both stdio.h and string.h and found nothing and so assume that index() must come from deep within compilers functions. (hence the 'built-in', duh*)

I just changed the name to myindex(). (hope you don't mind since it's really YOUR index*)
Another thing is that you didn't have a declaration for the function before main(), like this:
CPP / C++ / C Code:
#include <stdio.h>
#include <string.h>

void myindex(int argc, char *argv[]);               /* function declaration */

int main(int argc, char *argv[])      /* note the int return type for main */
{
    . . . . . .
    return 0;                                /* and the return value                 */
}
Another thing is that main() defaults to return type int, so you might as well write it that way and make sure main() returns some int. I usually just ' return 0; '
Now it compiles ok..... Outputs nothing...
Maybe someone else knows more about the built-in: index()

See what a difference it makes when you post your code?
Oh, and please DO use the c++ tag icon to enclose code included in your post. Not 'quote' and not 'code' . The tags should say cpp and /cpp, each in square brackets eg: [xxx] your code [/xxx] .
Howard;
Last edited by Howard_L : 25-Jul-2007 at 23:31.
  #6  
Old 26-Jul-2007, 01:05
1978929 1978929 is offline
New Member
 
Join Date: Jul 2007
Posts: 5
1978929 is on a distinguished road

Re: conflicting types


Thanks ur reply.now it is work correctly.Thank you very much again
  #7  
Old 26-Jul-2007, 08:23
Howard_L Howard_L is offline
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 470
Howard_L has a spectacular aura aboutHoward_L has a spectacular aura about

Re: conflicting types


Quote:
Thanks ur reply.now it is work correctly.Thank you very much again
This would be more legible to others if it were written:
Quote:
Thanks for your reply. Now it is working correctly. Thank you very much again.
Just like code, it works much better if you use the correct words and syntax.
Please don't use 'webspeak'. It doesn't take THAT much longer to write right. At least try.
Please read the guidelines.
  #8  
Old 26-Jul-2007, 09:37
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,712
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: conflicting types


Quote:
Originally Posted by Howard_L
Hmmm, in mingw I get the warning:
Code:
gcc -Wall -W -pedantic conflictypes_x1.c conflictypes_x1.c:4: warning: conflicting types for built-in function `index'
...so it would seem thet there is a 'built-in' function by that name already.

This is one of those little annoyances that I have with all compilers that I have used from time to time: putting non-standard library function prototypes in standard library headers.

There is no standard C library function named index. Never has been (not in our beloved K&R, either). There is, however a "legacy" UNIX function named index (from BSD, I'm thinking, but don't quote me on that). Now, there is a specification of <stdio.h> and a listing of functions therein in both the C89 and C99 standards. My feeling is that if a C compiler vendor supplies any library functions beyond the standard C library functions (for whatever standard they are trying hardest to comply with), that by default they should be not be prototyped in any of the standard library function headers and by default they should not be linked into ordinary user code.

I don't know about mingw, but for gcc/cygwin version 3.4.something there is a prototype in <stdio.h> that is surrounded by a conditional #indef __STRICT_ANSI...#endif. So one way to suppress the warning would be to use a command line with the -ansi switch. For gcc 4.something on my Linux systems, there is also a prototype for index in <stdio.h>. It's a little harder to understand what specific conditions are required to use or not use that prototype, but the -ansi switch will suppress the message again. (And with this switch, it will not try to link the built-in index function.) Or you could use either -std=c89 or -std=c99 to get it to compile and link OK.

Of course, it's probably easier just to rename the function in the user code, as you did, since the amount of code is so small.


My point: From a teaching point of view, I wish all compilers would have their strictest enforcement of ANSI and International Standards in place by default. See footnote. Then, more experienced developers or maintainers of legacy code with non-standard vendor-supplied functions could would use some command-line switch to get the compiler to compile and link these, but the "new guys" would only have to deal with their own stuff, not some old leftover baggage from BSD or whatever.

Quote:
Originally Posted by Howard_L
Another thing is that you didn't have a declaration for the function before main(), like this:

But the whole function was defined before main(), so no prototype is required. I always put main() first, for easier editing (I'm funny that way: I like the editor to open at the top of the file, and I like to see main() without scrolling down), so I always have the prototypes at the top (or in a header file if there are lots of them). However, lots of people prefer the other functions first then the main() at the bottom (probably a bunch of old Pascal programmers---or maybe their teachers were). Then, if they put the functions in the right order, they never need prototypes (as long as all of their functions are all in the same compilation unit).

Oh, yeah, before I forget: What the heck is the gcc built-in index function? It made it into the POSIX standard (in the "legacy" section). It is specified to be exactly the same as the standard library function strchr! I guess they put it in gcc because some legacy UNIX code uses it (Or, maybe it would be more polite to say that some legacy UNIX programmers wanted to make sure their new open-source code, written with legacy habits, would compile OK with GNU compilers without any pesky command-line switches, so the gcc project mavens put it in. Just guessing, of course.)

Regards,

Dave

Footnote:
As another example, Borland has always had functions like "strupr" and "strrev" in their <string.h>, and people learning C with these compilers are surprised and disappointed when they use another compiler and find that these are not available.

I personally wish that they (Borland, GNU, Microsoft, etc.) would take all non-standard library functions out of standard library headers and put them somewhere else.

Like <conio.h> and <dos.h>: when I see them in a program I know ---and the original programmer knows--- that the program will have some non-standard library functions. It's great when compiler vendors put in extra functions that can be used in any program, but I get tired of arguing with people who say that they have been, "programming for many years and I have always used strupr(). It is in <stdio.h>, so it must be a standard function!"
Last edited by davekw7x : 26-Jul-2007 at 10:10.
  #9  
Old 26-Jul-2007, 11:01
Howard_L Howard_L is offline
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 470
Howard_L has a spectacular aura aboutHoward_L has a spectacular aura about

Re: conflicting types


Quote:
But the whole function was defined before main()
My face is red! I must have inadvertantly put it up there when editing the code to place indents correctly so that I could read it.... Right... what you said.

I re-searched stdio.h and string.h for the string: 'index', and still find none.
They both include a _mingw.h which doesn't have it either. So ...I don't know....
I do see the strchr() in string.h though.... but have never used it.
Quote:
take all non-standard library functions out of standard library headers and put them somewhere else
I agree. Maybe have stdio.h and then stdio_mingw.h for the extras, Call our congressmen?

Good to think about these facts of C programming. Thanks for the discussion Dave,
Howard;
Last edited by Howard_L : 26-Jul-2007 at 11:38.
 
 

Recent GIDBlogMeeting the local Iraqis by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
What are abstract data types bhagwat_maimt C++ Forum 1 03-Jan-2007 08:51
Hostall.biz - Quality free hosting for all types of sites! searche Web Hosting Advertisements & Offers 0 10-Jul-2006 13:47
Types of Web Site Hosting Olga Elizarova Web Hosting Forum 1 15-Mar-2006 14:46
multiple types in one declaration ?! weeb0 C++ Forum 2 25-Jan-2006 20:23
Data types and Hardware dependencies karthikeyansen C Programming Language 2 03-Apr-2005 21:58

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 11:40.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.