gpt4 book ai didi

c - strtok_r 导致 "assignment makes pointer from integer without a cast"

转载 作者:太空狗 更新时间:2023-10-29 15:17:58 24 4
gpt4 key购买 nike

我正在尝试在 C 中对字符串进行标记化,并使用 strtok_r 将标记保存到多个变量中。据我所知,我完全按照记录使用它:

char *saveptr;

char *ticketuser = strtok_r(request, ":", &saveptr);
char *ticketservice = strtok_r(NULL, ":", &saveptr);
char *ticketkey = strtok_r(NULL, ":", &saveptr);
//And so on...

其中“请求”是一串以冒号分隔的标记。当我尝试编译时,我在分配其中一个字符串的每一行上都得到“赋值从整数生成指针而不进行强制转换”。由于 strtok_r 应该返回一个 char*,我看不出问题出在哪里。

编辑:这是我所有的代码:

#include <stdio.h>

char *add( char *request ) {

char *name = "add";
char *secret = "secret1";
char *failcode = "0:add:0";
char returncode[80];
char *saveptr;

char *username = strtok_r(request, ":", &saveptr);
char *servicename = strtok_r(NULL, ":", &saveptr);
int parameter1 = atoi(strtok_r(NULL, ":", &saveptr));
int parameter2 = atoi(strtok_r(NULL, ":", &saveptr));
int ticketlead1 = atoi(strtok_r(NULL, ":", &saveptr));
char *ticketuser = strtok_r(NULL, ":", &saveptr);
char *ticketservice = strtok_r(NULL, ":", &saveptr);
char *ticketkey = strtok_r(NULL, ":", &saveptr);

//Catch any issues with the request
if (strcmp(username,ticketuser) != 0){
printf("username did not match ticket username\n");
return failcode;
}//if
else if (strcmp(servicename,ticketservice) != 0){
printf("service name did not match ticket service name\n");
return failcode;
}//else if
else if (strcmp(secret,ticketkey) != 0){
printf("secret key did not match ticket secret key\n");
return failcode;
}//else if

//request was good, return value
else{
int val = parameter1 + parameter2;
sprintf(returncode, "1:add:%d", val);
return returncode;
}//else

}//add


int main( int argc, char **argv ) {

char *returned;
char *req = "user:serv:5:8:1:user:serv:secret1";
returned = add(req);

printf(returned);
printf("\n");

return 1;

}//main

最佳答案

答案在评论中找到:我失踪了#include <string.h>在文件的顶部。

编辑:我应该补充一点,除了上述问题之外,还有其他问题。首先,saveptr应该初始化为空。其次,正如 BLUEPIXY 指出的那样,returncode[]是局部变量。将其定义替换为 char *returncode = malloc ( . . . );

关于c - strtok_r 导致 "assignment makes pointer from integer without a cast",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17015466/

24 4 0
文章推荐: c - 如何从c中的数字中的最高有效数字开始获得不同的数字?
文章推荐: javascript - iframe 上的 Jquery 自定义滚动条
文章推荐: javascript - 解决 iPad 上 Mobile Safari 中第 0 个