yulian
yulian

Reputation: 1627

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
}

Upvotes: 0

Views: 458

Answers (3)

pinkpanther
pinkpanther

Reputation: 4808

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
}

Upvotes: 1

md5
md5

Reputation: 23707

The declaration:

char * removeSpaces(char *); 

Is different from the definition:

char * removeSpaces(char * str[0]);

Change the prototype:

char * removeSpaces(char *); 

Upvotes: 0

Adam Liss
Adam Liss

Reputation: 48310

When you declare a function, each parameter needs a type and a name. Your removeSpaces function takes a single parameter of type char *, so you'd declare it as

char * removeSpaces(char * str)

Remember, the function doesn't know that you'll be passing it the first element of an array; it just knows you'll give it a pointer to a string of characters.

It's also the convention in C to return a null pointer when its data is missing or would be invalid. So instead of returning -1 if there's an error, you should return NULL.

Upvotes: 0

Related Questions