简体   繁体   中英

Passing and returning a string pointer in a function

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.

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM