Reputation: 37
I am attempting to make a function that splits a sentence into an array of strings (being separated at each space). My current code looks as follows:
#include <stdio.h>
int main() {
void splitString(char);
char meow[] = "Hello my name is ****";
splitString(meow);
return 0;
}
void splitString(char theString[]) {
char* word = strtok(theString, " ");
while (word != NULL) {
printf("%s\n", word);
word = strtok(NULL, " ");
}
}
When I compile this code I get the following error:
main.c:19:6: error: conflicting types for ‘splitString’
void splitString(char theString[]) {
^~~~~~~~~~~
I was wondering if anyone knew how I can fix this error as I am not completely sure.
Upvotes: 0
Views: 261
Reputation: 35512
You forward-declare splitString
as void splitString(char)
when it should be void splitStirng(char[])
. Move it before main and change it:
#include <stdio.h>
#include <string.h>
void splitString(char[]);
int main() {
char meow[] = "Hello my name is ****";
splitString(meow);
return 0;
}
void splitString(char theString[]) {
char* word = strtok(theString, " ");
while (word != NULL) {
printf("%s\n", word);
word = strtok(NULL, " ");
}
}
Also include <string.h>
since you use strtok
.
Upvotes: 1
Reputation: 224864
Forward declaring a function inside main is weird; move that outside of main
.
char
and char []
are not the same type; change your forward declaration to match the actual signature of splitString
.
void splitString(char theString[]);
You could avoid forward declaring the function entirely by just moving it above main
.
Upvotes: 0