1

I'm new to C++ and I'm playing with pointers. I can't figure out why this piece of code doesn't work for me. Can you tell me what's wrong with it?

char * name = "dharman";
char *ptr = name+3;
*ptr = 'a';

printf("%s", name);

I get unhandled exception all the time.

1
  • You should enable compiler warnings - they can be very useful for finding mistakes, including this one. Even if your compiler is outdated enough to allow this dodgy conversion, it should still give a warning about it. Commented Nov 20, 2013 at 12:22

7 Answers 7

2

This alone is an error:

char * name = "dharman";

The string is in constant memory but the pointer's type indicates it can be modified. Attempting to modify it produces undefined behavior: on other platforms the program will work but you got unlucky.

This was a quirk in C++03; the newer C++11 spec makes it illegal. The reason it was ever done was C compatibility.

Whether you're writing in C++ or plain C, the solution is simple:

char name[] = "dharman";

Now the compiler stores the data in read-write memory because you have asked for an array of char, not a pointer to some other memory.

Sign up to request clarification or add additional context in comments.

Comments

1

String literals, like "dharman", are read-only and you cannot modify them. Instead, create and initialize an array that is not read only.

char name[] = "dharman";

Comments

1

name is a pointer to a string literal "dharman", which is located in read-only memory.
In your statement *ptr = 'a', you are trying to modify this string literal, which results in Undefined Behavior

Comments

0

It doesn't work because "dharman" is constant, it's a string literal. You cannont change it!

String literals are usually placed in read-only segments of memory.

2 Comments

How come it's constant, I didn't declare it as a constant.
@Dharman: String literals are constant - that's just how the language works.
0

You are trying to modify a constant string. You need to copy the constant first to some memory that you own. Try this:

char *name = (char*) malloc(10);
memcpy(name, "dharman", strlen("dharman"));
...

Comments

0

You are setting name to point at a const string and then trying to modify it. Copy the string to a modifiable location:

char *name = (char*) malloc(strlen("dharman") + 1);
memcpy("dharman", name, strlen("dharman") + 1);

Comments

-1

The reason for unhandled exception in your case is.In your code

char *ptr= name+3;

consider the base address of name as eg:23300, so char *ptr =name+3 will be equal to 23300+(3*sizeof(char)).so now ptr points to 23300+(3*1)=23303.each element occupies one bye for char so ptr will point to letter 'a' in "dharman".since "dharman" is char const you can't its value that's why you are getting error.if u remove the line *ptr=3.the code will work without out any issue.I hope you find this post useful.

1 Comment

That's the dumbest answer I ever got on SO!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.