I am quite new to programming in C and I have a problem that I have been trying to solve the last days, but now I am at a point where I don't know what to do.
I am reading in a string with the function "input" which gets then split into parts everytime there is a white space. The parts are stored in char arrays, which works fine so far. However, when I call the next function "checkInput" in main, the char arrays are empty again. What do I have to change, so that the char arrays are not empty when calling the next function?
When I used scanf instead of fgets, it worked. But why?
Any help would be much appreciated.
void input(char* string1, char* string2, char* string3)
{
char ptr[100];
printf("Enter String: \n");
fgets(ptr, 100, stdin);
printf("%s \n", ptr);
if(ptr != NULL)
{
string1 = strtok(ptr, " \n");
printf("string1: %s \n", string1);
}
if(ptr != NULL)
{
string2 = strtok(NULL, " \n");
printf("string2: %s \n", string2);
}
if(ptr != NULL)
{
string3 = strtok(NULL, " \n \0");
printf("string3: %s \n", string3);
}
}
int main(void)
{
char string1[100];
char string2[100];
char string3[100];
input(string1, string2, string3);
checkInput(string1, string2, string3);
return 0;
}