link
link

Reputation: 3

scanf_s error visual studio

int setN, setN2; char sign; scanf_s("do %d %c %d", &setN, &sign, &setN2);

I'm input "do 1 + 3", for example, and program in vs fall with an error "Unhandled exception at 0x650de541 in disc_II_2_1.exe: 0xC0000005: Access violation writing location 0xc96ff41e".

P.S. code below get the same result.

scanf_s("do %d %c %d", &setN, &sign, &setN2, 8);

What am I doing wrong?

Upvotes: 0

Views: 2777

Answers (1)

SleuthEye
SleuthEye

Reputation: 14579

From MSDN:

Unlike scanf and wscanf, scanf_s and wscanf_s require the buffer size to be specified for all input parameters of type c, C, s, S, or string control sets that are enclosed in []. The buffer size in characters is passed as an additional parameter immediately following the pointer to the buffer or variable.

and later

In the case of characters, a single character may be read as follows:

char c;
scanf_s("%c", &c, 1);

At the end of that reference, there are also a few examples where you may see that:

  • the count argument should appear immediately after the corresponding input
  • the count argument should correspond to the maximum number of expected char (or as stated above for a single char, it should be 1)

So, in your particular case you should have:

scanf_s("do %d %c %d", &setN, &sign, 1, &setN2);

Upvotes: 1

Related Questions