problem shows in visual studio

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;
}
Last edited on
write a simple C++ code

This is more like C than C++.

scanf_s("%s", &address);
You're trying to store an array of characters inside a single character, which is why you're getting access violation.
i am learning from my modul. it only teach C. %s is a string , not a single character. What is the wrong place?


%s is a string , not a single character. What is the wrong place?

Yes, and a string is an array of characters.

You're trying to fit a string into a single character.
1
2
3
char address;
printf("enter your address:");
scanf_s("%s", &address);
so, how to declare a string. i dun noe anything except char.
so, how to declare a string. i dun noe anything except char.
a string is an array of characters.
char name[] = "Teo Pei Shen";
ya. then how to write the declaration for address
 
char address[];


can?
Last edited on

ya. then how to write the declaration for address

char address[];


can?

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
const int address_len = 1000;
char address[address_len]{};    // make sure to initialise it 
Thnx for help. But what is the {} stand for?

Thnx for help. But what is the {} stand for?

Just a shorter way of writing
char address[address_len] = { 0 };
 
char address[address_len]={1000};

am i right?
or
 
char address[1000];
Last edited on
Exception thrown at 0x0F220B5C (ucrtbased.dll) in ConsoleApplication1.exe: 0xC0000005: Access violation writing location 0x011B0000.

If there is a handler for this exception, the program may be safely continued.

I just use your code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include "stdafx.h"


int main()
{
	const int address_len = 1000;
	char address[address_len]{};
	printf("where teo pei shen live?");
	scanf_s("%s", &address);
	printf("teopeishen live in %s", address);
    return 0;
}


Line 9: omit the &. Passing an array is usually implicitly passing an pointer to the first element.

scanf_s("%s", &address);

This &address is the address of an address.
 
scanf_s("%s",address);

why need to omit &, please explain. This &address is the address of an address.
What it means?
When you pass the variable address as a parameter to scanf_s(...) the type of the variable is implicitly converted to char *. Basically:

scanf_s("%s", address); -> scanf_s("%s",char *);

If you pass &address you pass the pointer to pointer provide by address. So

scanf_s("%s", &address); -> scanf_s("%s",char **);

Thus due to the added & scanf_s will overwrite the pointer (because it doesn't check the type) not the content which will crash.
what is the pointer ?char * means string?if double **, it will crash due to the become char back?
btw, I follow your guide and finish a program.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <stdio.h>
int main()
{
	const int answer_len=1000;
	char answer1[answer_len]{};
	char answer2[answer_len]{};
	char question1[] = "What is your name?";
	char question2[] = "where you live?";
	printf("%s\n",question1);
	scanf("%s",answer1);
	printf("%s\n",question2);
	scanf("%s",answer2);
	printf("Hello!%s from %s",answer1,answer2);
	return 0;
}


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?
Topic archived. No new replies allowed.