[英]Is it right to always keep the first parameter of strtok_s as NULL?
[英]strtok_s ignors first characters
我正在嘗試獲取輸入並將其傳遞給四個變量名。
我在每個strtok_s
之后進行檢查,看看我得到了什么,但第一個單詞只在 4 個字符之后才算。
我的代碼:
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;
}
strtok_s
的最后一個參數不正確,它應該是一個指針, strtok_s
使用它來存儲其內部 state。
您的 function 應該看起來更像這樣:
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
}
另一個問題(盡管不是主要問題)是 function 具有void
返回類型並返回int
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.