i write a simple C++ code and debug in visual studio. I got this error.
Exception thrown at 0x53380B5C (ucrtbased.dll) in ConsoleApplication1.exe: 0xC0000005: Access violation writing location 0x00FE0000.
1 2 3 4 5 6 7 8 9 10 11 12
#include "stdafx.h"
int main()
{
char name[] = "Teo Pei Shen";
char address;
printf("enter your address:");
scanf_s("%s", &address);
printf("%s is living in %s", name,address);
return 0;
}
The length of an array needs to be known at compile time, so what you have above will not compile.
You won't know how many characters the user will enter, which is why you use C++ I/O and strings. But if you insist on using C-style strings, just make the size of the array some large number.
1 2
constint address_len = 1000;
char address[address_len]{}; // make sure to initialise it
When i test it, it does not appear good. because address i type 11, jalan besar, tongkang pecah. but in the printf function it only appear 11, jalan and then end . why?