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
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.