0

I keep running into an "Access violation writing location" whenever I try to read an expression such as 3 + 5

#include <stdio.h>

add(double a, double b, int prec);

int main() {
    int prec;
    double a, b;
    char oper;

    printf("Enter Precision: ");
    scanf_s("%d", &prec);
    if (prec<0) {
        printf("This is not a valid precision value");
    }
    printf("%d", prec);
    printf("Enter Expression: ");
    scanf_s("%lf %c %lf", &a, &oper, &b);
    …
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 1
    Post your whole code! – Box Box Box Box Feb 18 '16 at 05:02
  • 3
    @AshishAhuja No, post an MCVE. – Michael Albers Feb 18 '16 at 05:05
  • You should end outputs (such the precision output) with newlines. It's also worth annotating the printed values (`printf("Precision: %d\n", prec);`). – Jonathan Leffler Feb 18 '16 at 05:07
  • @MichaelAlbers, yes I know that a MCVE is supposed to be posted, but the code the user had given doesn't even compile not because of small problems, but he has not posted functions he is using. Like `add` is declared but not defined. So I have to delete that line. Then I had to add brackets to successfully compile it. Now it is edited, but still not an MCVE. It is not Complete and Verifiable. It is just an Example and Minimal. – Box Box Box Box Feb 18 '16 at 05:07
  • The code is not too far off an MCVE ([MCVE]). The variable `prec` and the code that manages it is not relevant if the problem is reading the expression — so it should be omitted, really. The declaration of `add` is superfluous to the question. And it would be worth adding a `printf()` to echo the expression if the read is successful (in lieu of the `…` that I added). Then it would be an MCVE. – Jonathan Leffler Feb 18 '16 at 05:10
  • 1
    @AshishAhuja: Actually, `add` is declared but not defined — and the lack of definition doesn't matter since it isn't used. – Jonathan Leffler Feb 18 '16 at 05:11

1 Answers1

7

According to MSDN, since you are using scantf_s function with %c format, you have to specify character buffer length:

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.

So correct method call would be:

scanf_s("%lf %c %lf", &a, &oper, 1, &b);
Ari0nhh
  • 5,720
  • 3
  • 28
  • 33
  • I think you're right, but it seems odd that a buffer size would be required for c/C formats. It makes sense for s/S of course. – Tom Karzes Feb 18 '16 at 05:19
  • 1
    @TomKarzes Thats because you could read multiple characters using `scanf_s` with `%c`: `char c[4]; scanf_s("%4c", &c, _countof(c));` – Ari0nhh Feb 18 '16 at 05:24
  • 2
    @TomKarzes: Note that you can use `"%12c"` as a format, and then `scanf()` expects a pointer to at least 12 characters — it does not null terminate the data (so you might need a `%n` immediately after it so you know how many characters were read). It doesn't skip leading blanks, and it does not stop because of white space. – Jonathan Leffler Feb 18 '16 at 05:24
  • Ah, ok, now that makes sense! – Tom Karzes Feb 18 '16 at 05:33