Reputation: 1627
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
Reputation: 4808
Your code has two issues:
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.
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.
-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
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
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