2

I'm trying to get an input and it to four variable names.

I'm doing a checkup after every strtok_s to see what I got but the first word only counts after 4 characters.

My code:

void zeros()
{
    char buffer[81];
    fgets(buffer, sizeof(buffer), stdin);
    printf("%s\n", buffer);
    char *command = strtok_s(buffer, " \t", &buffer);
    printf_s("the command you selcted is %s\n", command);
    char *Matname = strtok_s(NULL, " \t", &buffer);
    printf_s("the name you selcted is %s\n", Matname);
    char *row = strtok_s(NULL, "  \t", &buffer);
    printf_s("the rowsize you selcted is %s\n", row);
    char *col = strtok_s(NULL, " \t", &buffer);
    printf_s("the colsize you selcted is %s\n", col);
    return 0;
}
anastaciu
  • 23,467
  • 7
  • 28
  • 53

1 Answers1

1

The last argument of strtok_s is not correct, it should be a pointer which is used by strtok_s to store its internal state.

Your function should look more like this:

void zeros() // or int zeros() if it's supposed to return int
{
    char buffer[81];
    char *ptr;
    fgets(buffer, sizeof buffer, stdin);
    printf("%s\n", buffer);
    char *command = strtok_s(buffer, " \t", &ptr);
    printf_s("the command you selcted is %s\n", command);
    char *Matname = strtok_s(NULL, " \t", &ptr);
    printf_s("the name you selcted is %s\n", Matname);
    char *row = strtok_s(NULL, "  \t", &ptr);
    printf_s("the rowsize you selcted is %s\n", row);
    char *col = strtok_s(NULL, " \t", &ptr);
    printf_s("the colsize you selcted is %s\n", col);
    // return 0; // if function return type is int
}

Another issue, albeit not the main one, is that the function has void return type and returns an int.

anastaciu
  • 23,467
  • 7
  • 28
  • 53
  • 1
    i want to to get the int later by taking the int size using strtol on the row and col. and i cant understand why did you used another char var called *ptr. edit : it worked !! thank you very much :) – Yuval Grimberg May 01 '21 at 14:07
  • no no, i need to make a large terminal that the function zeros is a function when the user enters an input that is combined by a command, matrix name and size of the matrix. i have the "big" string and know i want add few more line of code that i'll have the int size of row and col. the print functions is to make sure what the program got. – Yuval Grimberg May 01 '21 at 14:21
  • @YuvalGrimberg ok then, good luck, if you have any more problems, we're here to help. – anastaciu May 01 '21 at 14:28
  • i've continued with my project and i've made some changes to the code, i added in the end strtol so that i'll get 2 char and 2 ints. for some reason the two ints (row and col) are usubale, the two char variables are being deleted. do you have any idea why? – Yuval Grimberg May 03 '21 at 08:49
  • @YuvalGrimberg, it's hard to tell without the code, those are pointers, if you reassing them the data where they were pointing before is lost. You can't return multiple different values in C, you can either make pointer arguments and assign those values to the pointers so that the caller retains them, or make a struct with 2 chars and 2 ints, and return that, [check this post](https://stackoverflow.com/q/2620146/6865932) for some intructions on how to do it, if you still can't, ask a new question with your code. – anastaciu May 03 '21 at 08:58
  • @YuvalGrimberg, this answer in another post is more detailed https://stackoverflow.com/a/3626699/6865932 – anastaciu May 03 '21 at 09:04