c - 在c中的函数中使用 token (strtok)的值

标签 c function token strtok

我对 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/

相关文章:

django - 使用 token 身份验证时如何在 django channel 中的 websocket 连接中对用户进行身份验证

C++ 标记字符串

c - 将非数组变量的地址传递给声明为 `Type ptr[static 1]` 的函数参数是否有效?

javascript - 依次调用 Angular2 函数

ajax - 在没有提交按钮的情况下防止针对ajax请求和表单的CSRF

c - 从 64 位值获取最后两个字节

javascript - Settimeout() javascript 函数被忽略

c - 使用一个名称的全局链接变量

c - 取消引用递增的指针会导致指针更改其值吗?

c - 如何消除与大纪元时间的时间差异?