简体   繁体   English

C sscanf:为什么会出现段错误?

[英]C sscanf: why is this segfaulting?

I'm trying to take tokens in a format of "%i / %i%s" and split them into three variables. 我正在尝试采用“%i /%i%s”格式的令牌,并将其分为三个变量。

char char1[20];
int int1;
int int2;


sscanf(token, "%[^/]/%d", char1, &int2);
printf("%s - %i ", char1, &int2);

It just segfaults. 它只是段错误。 What am I doing wrong? 我究竟做错了什么?

I've tried changing %d to %i, with no difference. 我尝试将%d更改为%i,没有任何区别。

You shouldn't print address of int2 in your printf. 您不应在printf中打印int2的地址。 This is working for me: 这为我工作:

const char *token = "qwerasdf/10";
char char1[20];
int int2;

sscanf(token, "%[^/]/%d", char1, &int2);
printf("%s - %i ", char1, int2);

Output: 输出:

qwerasdf - 10 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM