firstaccount
firstaccount

Reputation: 163

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 ?

Upvotes: 0

Views: 556

Answers (1)

krzaq
krzaq

Reputation: 16421

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);

Upvotes: 3

Related Questions