problem shows in visual studio

Feb 22, 2017 at 6:41am
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 Feb 22, 2017 at 8:03am
Feb 22, 2017 at 6:54am
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.
Feb 22, 2017 at 6:57am
i am learning from my modul. it only teach C. %s is a string , not a single character. What is the wrong place?
Feb 22, 2017 at 7:30am


%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);
Feb 22, 2017 at 7:43am
so, how to declare a string. i dun noe anything except char.
Feb 22, 2017 at 7:51am
so, how to declare a string. i dun noe anything except char.
a string is an array of characters.
char name[] = "Teo Pei Shen";
Feb 22, 2017 at 8:01am
ya. then how to write the declaration for address
 
char address[];


can?
Last edited on Feb 22, 2017 at 8:02am
Feb 22, 2017 at 8:52am

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 
Feb 22, 2017 at 9:04am
Thnx for help. But what is the {} stand for?
Feb 22, 2017 at 9:08am

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

Just a shorter way of writing
char address[address_len] = { 0 };
Feb 22, 2017 at 12:51pm
 
char address[address_len]={1000};

am i right?
or
 
char address[1000];
Last edited on Feb 22, 2017 at 12:54pm
Feb 22, 2017 at 2:24pm
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;
}


Feb 22, 2017 at 3:08pm
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.
Feb 23, 2017 at 6:12am
 
scanf_s("%s",address);

why need to omit &, please explain. This &address is the address of an address.
What it means?
Feb 23, 2017 at 11:30am
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.
Feb 24, 2017 at 6:21am
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.