简体   繁体   中英

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.

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.

You shouldn't print address of int2 in your printf. 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 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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