What type of argument does sscanf expect for a character class?

I am trying to get sscanfto recognize a fairly simple format using character classes. I noticed that when I provide sscanfwith char*to match a character class, it overwrites the previous byte just as if it were expecting a pointer to 2 bytes.

A simplified version of what I'm trying to accomplish:

#include <stdio.h>

int main(void)
{
    char num1;
    char num2;
    int s;
    s = sscanf("1,2", " %[01234567] , %[01234567]", &num1, &num2);
    printf("%d %c %c\n", s, num1, num2);
    return 0;
}

Expected Result: 2 1 2

Actual output: 2 2

But if I replaced charwith short(or something larger than a byte), then it works as expected, but I get warnings about format expects type char*.

What type should be the argument really or am I making some other mistake?

+3
source share
1

sscanf .

char num1[BIG_ENOUGH], num2[BIG_ENOUGH];
s = sscanf("1,2", " %[01234567] , %[01234567]", num1, num2);

, , .

, C , .

+3

Source: https://habr.com/ru/post/1772342/


All Articles