-1

I call my function from my main as so...

int main() {

   char* input[81];
   char** array[81];

   userInput(input);

   parseInput(input);

}

userInput is taking in input and storing in the input variable. this is working correctly.

but now I want to send both the input and a char pointer array to my parseInput function to parse the input into an array using strtok

the following is my parseInput() function...

void parseInput(char* in) {

   char *tok = strtok(in, " ");

   int i = 0;
   while(tok != NULL) {

      array[i] = tok;
      tok = strtok(NULL, " ");
      i++;

   }

}

when I try to compile I am getting the following warning and I do not understand why.

warning: passing argument 1 of ‘userInput’ from incompatible pointer type [enabled by default] userInput(input);

warning: passing argument 1 of ‘parseInput’ from incompatible pointer type [enabled by default] parseInput(input, array);

Any guidance would be greatly appreciated. thank you

lizard
  • 13
  • 4
  • 1
    1 pointer to char, is different to 81 pointers to char . To help us understand what you are trying to do, it would help to show the `userInput` function. Also, the `parseInput` function should fail to compile due to `array` not being visible – M.M Jun 14 '21 at 00:06

1 Answers1

2

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.
Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39