type mismatch in redeclaration of remove

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tan007
    New Member
    • Sep 2011
    • 6

    type mismatch in redeclaration of remove

    Code:
    #include<stdio.h>
    int remove(char s[]);
    int getline(char line[],int len);
    
    main()
    {
        int len;
        len=0;
         char line[100];
        while(getline(line,len)>0)
            if(remove(line)>0)
           printf("%s",line);
    }
    
    int remove(char s[])
    {
         int i;
          i=0;
         while(s[i]!='\n')
           ++i;
       return i;
    }
    this is my half program...i have just started programming in c...m getting an error that type mismatch in redeclaration of remove...can some one help me in rectifying the error..
  • alexis4
    New Member
    • Dec 2009
    • 113

    #2
    Where is the getline() function?

    Comment

    • alexis4
      New Member
      • Dec 2009
      • 113

      #3
      I wrote a dummy getline() function and compiled just fine with my compiler. It seems that this way of function call is not supported by your compiler.

      I then compiled it with strict ISO/ANSI and got an error at line 9:

      Error[Pe268]: declaration may not appear after executable statement in block
      Which means that you have to change line 8 and 9 between them because a declaration is forbidden after executable code. But you are saying that you are getting another error.

      Just googled it:

      Check out this link here, it tells you clearly what to do:

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        remove is a C standard library function declared in stdio.h as

        int remove ( const char * filename );


        You should use a different name for this function in your code. Presumably you got the error at line 3. Since this is your first line of code that should have been an indication that it was something to do with the stdio.h header.

        You need to learn what is in the standard library so you can avoid these conflicts.

        Comment

        • tan007
          New Member
          • Sep 2011
          • 6

          #5
          my getline function takes input from the user...no problem in that....
          and i have used name remove in many other prog..just works fine...
          my whole prog is correct and even my function prototype and definition...i have used the same function in other program it works fine..only in this program its giving me an error...

          Comment

          • tan007
            New Member
            • Sep 2011
            • 6

            #6
            Code:
            #include<stdio.h>
            #define MAXLINE 100
            /*exercise 1.18 */
            int getline(char line[],int maxline);
            int remove(char s[]);
            
            /*print longest line input*/
            main()
            {
            	int r;
            	int len;  /*current line length*/
            	int max;  /*maximum length seen so far*/
            	char line[MAXLINE];
            	char longest[MAXLINE];
            
            	max=0;
            	while((len = getline(line,MAXLINE)) >0)
            	  {
            		      r=remove(line);
            		      if(r>0)
            		       printf("%s",line);
            	 }
            	return 0;
            }
            
            /*getline:read a line into s,return length*/
            int getline(char s[],int lim)
            {
            	int c,i,j;
            	j=0;
            	for(i=0; (c=getchar())!=EOF && c!='\n';++i)
            		if(i<lim-2)
            		{
            		s[j]=c;
            		++j;
            		}
            		if(c=='\n')
            		{
            			s[j]=c;
            			++j;
            			++i;
            		 }
            	s[j]='\0';
            	return i;
            }
            
            /*remove the tabs and blanks*/
            int  remove(char s[])
            {
            	int i;
            	i=0;
            	while(s[i]!='\n')
            		++i;
            	--i;
            	while(i>=0 && (s[i]==' ' || s[i]=='\t'))
            		--i;
            	 if(i>=0){
            		++i;
            		s[i]='\n';
            		++i;
            		s[i]='\0';
            		}
            
                  return i;
            }

            this is my entire program....plz check if u can find any error....m getting an error....type mismatch in declaration of remove
            Last edited by Niheel; Sep 12 '11, 12:50 AM. Reason: code tags

            Comment

            • Banfa
              Recognized Expert Expert
              • Feb 2006
              • 9067

              #7
              I'm not all that worried about what you have done previously, here you are getting a type mismatch because you have declared remove differently to how it is declared in stdio.h.

              I suggest you get out of the habit of using the name remove for your own functions since it is a standard library function. At some point (when one of your programs needs to call standard library remove) this is going to trip you up and cause very unexpected results.

              Comment

              • tan007
                New Member
                • Sep 2011
                • 6

                #8
                thnks banfa.....my error got solved....
                Last edited by Niheel; Oct 1 '11, 09:03 PM.

                Comment

                Working...