firstaccount firstaccount - 1 month ago 10
C++ Question

sscanf_s access violation when seperate string and integer

char a[200] = { 0 };

char tst[20] = "aaaa 123\n";
int i;
sscanf_s(tst, "%s %d",a, &i);
printf("reasult:%s %d", a,i);


No matter I use
char tst[20] = "aaaa 123\n";
or
char* tst = "aaaa 123\n";
,
it always shows access violation.
I need to seperate a string an integer from a string. But why this happens ?

Answer

sscanf_s expects two arguments for %c, %s and %[, the second being the size of the buffer passed. The following should work:

sscanf_s(tst, "%s %d", a, sizeof(a), &i);