我对 token 的使用有点困惑,假设:
int main(void) {
char input[100];
fgets(input, 100, stdin);
char * token = strtok(input, " ");
char * height = strtok(NULL, " ");
char * width = strtok(NULL, " ");
if (height > 9 && width > 9)
set(height, width);
}
void set(char * height, char * width) {
for (int i = 0; i < height + 1; i++) {
for (int j = 0; j < width + 1; j++) {
mine[i][j] = '*';
}
}
}
我刚刚发现我不能使用“height+1”,谁能告诉我任何使用高度值的方法吗?另外,我应该把 char *height 和 char *width 放在 void set 中吗?
谢谢!
最佳答案
使用 fgets()
从流中读取数据并对其进行标记后,您应该将字符串转换为适当的整数值。
char *heightStr = strtok(NULL, " ");
char *widthStr = strtok(NULL, " ");
int heightVal = atoi(heightStr);
int widthVal = atoi(widthStr);
您的 set()
函数还应该使用整数作为参数类型,而不是 char*
即。 void set(int 高度,int 宽度)
关于c - 在c中的函数中使用 token (strtok)的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36357664/