Dash
Dash

Reputation: 37

Conflicting Types for C program

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

Answers (2)

Aplet123
Aplet123

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

Carl Norum
Carl Norum

Reputation: 224864

  1. Forward declaring a function inside main is weird; move that outside of main.

  2. char and char [] are not the same type; change your forward declaration to match the actual signature of splitString.

    void splitString(char theString[]);
    
  3. You could avoid forward declaring the function entirely by just moving it above main.

Upvotes: 0

Related Questions