Search code examples
cstringstrdup

error: conflicting types for 'removeSpaces'


I want to write a program which will verify whether the string is palindrome or not. But there is an error when I try to pass strings[0] to removeSpaces function which will remove spaces.

Why does 'comflicting types error' occurs? What is wrong?

The whole code of programm:

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

char * removeSpaces(char *); // prototype

int main()
{
    char *strings[2]; // created array of pointers

    strings[0] = strdup("a man a plan a canal panama");
        printf("%s\n", strings[0]);

    strings[1] = removeSpaces(strings[0]);
        printf("%s\n", strings[0]); 
        /* (in future) it will display "amanaplanacanalpanama" */

    free(strings[0]);

    return 0;
}


char * removeSpaces(char * str[0]) // an ERROR occurs here 
{
    /* some code */

    return -1; // in case of fault
}

Solution

  • Your code has two issues:

    1. Your function declaration and definition conflicted. You don't have to specify char *str[0], char *str is enough and it should match with your declaration at the top.

    2. You are returning -1 instead of a pointer which is not a valid pointer in its form. If some fault occurs I would recommend you to return NULL instead.

    3. -1 is an integer. But however, you can also use 0 instead, because that defaults to (void *)0 nothing but NULL.

    change like this:

     char * removeSpaces(char * str) // an ERROR occurs here 
    {
        /* some code */
    
        return NULL; // return NULL in case of fault instead of -1
    }