Access violation writing location 0x00D00000

前端 未结 1 925
逝去的感伤
逝去的感伤 2021-01-29 12:47

I am trying to write this simple program which reads multiple variables in one scanf function but an exception is thrown after I enter the input?

Edit: I used scanf init

相关标签:
1条回答
  • 2021-01-29 13:23

    The %s format specifier to scanf/scanf_s expects a pointer to the first element of an array of char, i.e. a char *. Instead, you're passing in the address of an array, in this case a char (*)[20]. Those types are incompatible. Passing the the wrong type for a format specifier invokes undefined behavior.

    Instead of passing in &name, pass in name. When passed to a function, an array decays to a pointer to the first element, so this is the proper type.

    scanf_s("%19s %d %c %f", name, sizeof(name), &age, &gender, sizeof(gender), &income);
    

    Also note the length specifier given, which limits the number of characters that can be read in so it doesn't try to write past the end of the array.

    0 讨论(0)
提交回复
热议问题