HB-
HB-

Reputation: 627

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.

Upvotes: 0

Views: 75

Answers (1)

Krypton
Krypton

Reputation: 3325

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 

Upvotes: 2

Related Questions