gpt4 book ai didi

c - 将 char 数组传递给函数

转载 作者:太空宇宙 更新时间:2023-11-04 08:53:29 24 4
gpt4 key购买 nike

可怕的警告:传递参数 1 的 ... 从指针生成整数而不进行强制转换

我只是不明白这一点。我想要做的就是将一个简单的字符串(是的,我知道一个字符数组)传递给一个函数。该函数解析字符串并返回第一部分。有人可以告诉我我做错了什么吗?谢谢!

char* get_request_type(char* buffer) {
char* p;
p = strtok(buffer, "|");
return p;
}

int main()
{
char buffer[30] = "test|something";
fprintf(stdout, "buffer: %s\n", buffer); //<-- looks great but needs parsing
char* request_type = get_request_type(buffer); //<-- error is here
fprintf(stdout, "%s\n", request_type); //<--expecting to see 'test'

}

可能有一天我会习惯于在 C 中使用字符串,但那一天不是今天......

最佳答案

您没有包含 string.h,因此编译器将假定 strtok 定义为:

int strtok();

返回 int 并接受未知数量的参数,它调用隐式函数声明。解决方案是在声明 strtok 的地方包含 string.h

关于c - 将 char 数组传递给函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18604114/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com