0

I'm doing a project that requires me to open a file and analyze it in some way. I've made an array of all the lines and right now I'm trying to send one of the lines to a function and to return a pointer value to an array that I'm building in the function. this is the code for the function

    char** colornamepicker(char** userinput) {//stringRemoveNonAlphaNum is a diffrent function that cleans the string
    char* ptr;  ptr = 0;
    char* Color = strtok_s(userinput, " \t", &ptr);
    Color = stringRemoveNonAlphaNum(Color);
    char* first_name = strtok_s(NULL, " \t", &ptr);
    first_name = stringRemoveNonAlphaNum(first_name);
    char* last_name = strtok_s(NULL, " \t", &ptr);
    last_name = stringRemoveNonAlphaNum(last_name);
    char** player[3];//this is the point when im bulding the array and the values are good.
    player[0] = _strdup(Color);
    player[1] = _strdup(first_name);
    player[2] = _strdup(last_name);
    return *player;//Im returing a pointer because that what worked with some of the problems in the main
}

and here is the part of the function from the main that doesn't work, im trying to print it again to see what happend.

    main(){
            ...//bunch of irrelevant code that opens a file and turns every line to an array of strings
        char** player_1 = colornamepicker(line[1]);
        printf_s("\nnow its the time to see if everything worked");
        printf_s("\n%s %s %s" ,player_1[0],player_1[1],player_1[2]);
        free(player_1);
    }

Thanks for the help !

  • This should be spewing warnings. `player[0]` for example, is a `char**`, you're assigning a `char*` and that alone should be a huge tell that something is wrong. If you're not getting a flood of warnings (that should *all* be treated as fatal errors because that's exactly what they are) you need to turn up your compiler warnings. and `strtok_s` doens't take a `char**` for its first argument; it takes a `char*` – WhozCraig May 19 '21 at 11:48
  • Does your `player` var exists outside of `colornamepicker` ? – GabrielT May 19 '21 at 11:48
  • You've been told before about [minimal complete examples](https://stackoverflow.com/help/minimal-reproducible-example), and variable scope, and the technique of passing pointers to a function. You must make an effort to learn C, not just ask for help when your code doesn't work. – Beta May 19 '21 at 11:49
  • @GabrielT since `player` is declared on the ninth line of that function in very code the OP has posted, I'm going to go ahead and say "no". – WhozCraig May 19 '21 at 11:50
  • Wrong type, invalid return : `char** player[3];` -> `char** player = malloc(3*sizeof(*player));` – 0___________ May 19 '21 at 11:53

0 Answers0