You defined the function parseInput
as taking a parameter of type char*
, but in the function main
, you are passing it a decayed parameter of type char**
. That is why your compiler is complaining about an incompatible pointer type.
Instead of
char* input[81];
you should probably write:
char input[81];
Also, the declaration
char** array[81];
should probably be changed to:
char* array[81];
Also, as already pointed out in the comments section, the function parseInput
will not compile, because it refers to an identifier array
, but no identifier with that name is visible to that function. It cannot see the identifier array
of the function main
. Therefore, if you want the function parseInput
to have access to array
in the function main
, then, when calling parseInput
, you must pass a pointer to that array.
An additional problem with your code is that the behavior of the function parseInput
does not make sense, as the caller of that function (in this case main
) has no way of knowing how many elements of the pointer array were written by parseInput
. Therefore, it has no way of knowing how many elements of the array are valid after the function call. For this reason, it would probably make more sense to make the function parseInput
return an int
which specifies the number of array elements written.
Another problem is that the function parseInput
simply assumes that the passed array is large enough. If that assumption is false, then it will cause a buffer overflow. Therefore, it would probably be safer to design the function parseInput
in such a way that it is also passed an argument which specifies the size of the passed array, so that the function can stop before a buffer overflow occurs.
After applying all of the modifications mentioned above, the function parseInput
would look like this:
int parseInput( char* in, char** results, int results_length )
{
char *tok = strtok(in, " ");
int i = 0;
while( tok != NULL && i < results_length )
{
results[i] = tok;
tok = strtok(NULL, " ");
i++;
}
return i;
}
Instead of calling the function like this
parseInput(input);
you would now call it like this:
int main(void)
{
char input[81] = "This is a test.";
char* array[81];
int num_elements;
num_elements = parseInput( input, array, sizeof array / sizeof *array );
for ( int i = 0; i < num_elements; i++ )
{
printf( "%s\n", array[i] );
}
return 0;
}
This test program provides the following output:
This
is
a
test.