Access Violation in C

Jun 24, 2020 at 4:50pm
Hello,

I have the below error: (line 8)
Exception thrown at 0x7928E63C (ucrtbased.dll) in C_1.exe: 0xC0000005: Access violation writing location 0x00BA0000. occurred

How can I fix this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <stdio.h>
#include<stdlib.h>


void getName(char names[][100], int i)
{
	printf_s("Enter Name: ");
	scanf_s("%s", names[i]);
}
void getFamily(char families[][100], int i)
{
	printf_s("Enter Family: ");
	scanf_s("%s", families[i]);

}
void Print(char names[][100], char families[][100], int i)
{
	printf_s("%s", names[i]);
	printf_s("\t");
	printf_s("%s", families[i]);
	printf_s("\n");
}

int main()
{
	char names[10][100];
	char families[10][100];

	for (int i = 0; i < 3; i++)
	{
		getName(names, i);
		getFamily(families, i);
	}
	printf_s("\n");
	for (int i = 0; i < 3; i++)
	{
		Print(names, families, i);
	}

	return 0;
}
Last edited on Jun 24, 2020 at 4:50pm
Jun 24, 2020 at 4:56pm
Use string instead of char arrays. They manage their own memory.
Jun 24, 2020 at 5:05pm
@Repeater
Thank you, but I don't want to use C++.
I want to use char of array.
Jun 24, 2020 at 5:08pm
What input values are you entering that breaks it? You're probably overflowing the arrays. Don't do that. Don't try to store more characters in the arrays than they have space for.
Jun 24, 2020 at 5:10pm
scanf_s may be 'safer', but that doesn't mean you can stop paying attention.

For one thing, you don't just tack on an "_s" to the function call, and voila, instant safety.

https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/scanf-s-scanf-s-l-wscanf-s-wscanf-s-l?view=vs-2019
The buffer size parameters you completely forgot will be garbage.

That VS still can't diagnose that you're not even remotely using the function correctly is another fail.
Jun 24, 2020 at 5:10pm
main.cpp(9): warning C4473: 'scanf_s' : not enough arguments passed for format string
main.cpp(9): note: placeholders and their parameters expect 2 variadic arguments, but 1 were provided
main.cpp(9): note: the missing variadic argument 2 is required by format string '%s'
main.cpp(9): note: this argument is used as a buffer size
main.cpp(14): warning C4473: 'scanf_s' : not enough arguments passed for format string
main.cpp(14): note: placeholders and their parameters expect 2 variadic arguments, but 1 were provided
main.cpp(14): note: the missing variadic argument 2 is required by format string '%s'

VS diagnoses it fine (although I'm compiling in C++).
Last edited on Jun 24, 2020 at 5:15pm
Jun 24, 2020 at 5:26pm
Good to know - thanks Ganado.

Topic archived. No new replies allowed.