0

I am new to the concept of character pointers and strings.So, having trouble in passing a string pointer and returning a string pointer in a function.the function is "remove".

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

char* remove(char* ,char ,int );
int main(){
    int count, i;
    char ch;
    char* a;
    char* b;
    b=(char*)malloc(sizeof(char)*10);
    a=(char*)malloc(sizeof(char)*10);
    gets(a);
    for(i=0;*a='\0';i++)
    {
        ch=a[i];
        b=remove(&a[i],ch,i);
    }
}

char* remove(char* str,char x,int k)
{
    char* str2=(char*)malloc(sizeof(char)*10);

    int i,j;
    j=0;
    int len;
    len=strlen(str);
    for(i=k;i<len;k++)
    {
        if(str[i]!='x')
        {
            str2[j]=str[i];
            j++;
        }
    }
    return str2;
}

The errors which i am getting are

error:conflicting types for 'remove'

In the line where the function was declared and defined in the subsequent line.

6
  • Doesn't the rest of the error say: /usr/include/stdio.h:178:12: note: previous declaration of ‘remove’ was here? Commented Sep 6, 2016 at 5:32
  • 2
    It's not the 1970s any more. You can write a = malloc(10) ; and the gets function no longer exists. Commented Sep 6, 2016 at 5:42
  • Your remove function fails to null terminate str2 Commented Sep 6, 2016 at 5:43
  • for(i=k;i<len;k++) Are you sure that you want to increment k ? Commented Sep 6, 2016 at 6:00
  • oh yea, it was a silly mistake.Got it! Commented Sep 6, 2016 at 6:12

2 Answers 2

3

Function remove() is already defined in stdio.h.

int remove(const char *pathname);

Please give some other name to your function char* remove().

Note : Whenever you get such error, please try to look at manual page. If you are in unix. Simply type man func_name in terminal.

Sign up to request clarification or add additional context in comments.

Comments

1

remove() is a standard function. You need to choose a different name for your remove() function name. Because your function definition for remove() differs with the prototype found in stdio.h.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.