why is this happening?

if i run

char * terry="hjk";
std:out << *terry;
std:out << ++(*terry);

it prints h and hangs out

But if i do this way:

char * terry;
char b[3]={'h','j','k'}
terry=b;
std:out << *terry;
std:out << ++(*terry);

it gives o/p : hi

Can anyone explain why this is happening?
Here

char * terry="hjk";
the compiler usually place the string literal in a read-only memory. So it would be more correctly to define

const char * terry="hjk";

Th C++ Standard allows the first record only for compatibility however you may not change the string literal. So here

std:out << ++(*terry);

you are trying to change the string literal. The behavior is undefined. In you case execution hangs out.

In the second case you create an array that may be changed.
Last edited on
ok thx i got it.
Yes, in C++ string literals have type const char[]. So you can only read them.
On the other hand the record

char s[] = "String literal";


means that a char array is created elements of which are initialized from characters of the string literal. As this array s has no const qualifier you can change it.
@vlad. ... So this is a difference b/w pointers and array?
if we define string literal as a char array. We can change it.But if declare/initialize as char pointer, we can't change its value.
I think that "String literal" is the same constant string literal regardless of the context.
But if you "give" it to a pointer, the pointer will only take its address.
While "giving" it to an array, the array copies its contents.
if we define string literal as a char array. We can change it.


No.

Anything in your code that looks like this:

"some letters"

is a string literal. It is stored in the compiled, built code looking just like that. You can open your executable with a hex editor and find it. It cannot be changed.

In your code above, the first example has a string literal. The second example does not. If you want to fiddle with string literals, you must arrange things such that you make a copy of it, and then you can fiddle with that copy.
Last edited on
There is a difference between two declarations

const char *s = "String literal";
and
char s[] = "String literal";

In the first case the compiler allocates memory for string literal the same way as for array usually in a read-only memory and assigns s address of the first character of the literal.

In the second case the compiler does not allocate memory for the string literal. It allocates memory only for the array s and initialize its elements with characters from the string literal.
Last edited on
@vlad. Ok it means

If I execute :

char *terry2="ikk";
std::cout<<++(*terry2);

it hangs out.Because it is trying to modify string literal.

But if i assign string to array first. Afterwards copy array to pointer and again perform operation on pointer as follows:

char terry[]="ikk";
char *terry2="ppp";
terry2=terry;
std::cout <<++ (*terry2);

Then it is working fine and gives o/p "j". Because now it is accessing array elements.
Last edited on
Yes, terry2 gets the address of the terry which is not a const array. However it would be more correctly to define terry2 as

const char *terry2="ppp";

because it points to a string literal. After that and new assignmnet

terry2=terry;

the statement

std::cout <<++ (*terry2);

also will generate an error.
Last edited on
Topic archived. No new replies allowed.