Well see the above code

int a = 10;
int* p1 = &a;
int* p2 = p1; // Copies the address right?

But what happens here?

char* str1 = "Hello";
char* str2 = str1; // What happens here

I tried this code and when I modified str1, str2 remain unchanged why?

One more thing, if I initialize something else to str1 i.e.

char* str1 = "Hello";
str1 = "Hell";

What happens here? Is the "Hello" string deleted and space for new string is accommodated?

Recommended Answers

All 4 Replies

What happens - see my post in the recent thread http://www.daniweb.com/forums/thread156711.html.
You can't modify string literal via pointers, on modern platforms you catch memory access exception. So in your 2nd snippet str2 points to the same C-string literal address - that's all. If you modify str1 value (for example, str1 = "Bye"; ), it does not bear a relation to its original target (->"Hello") or another pointer variable str2 value.

Pointers str1 and str2 are variables which contain addersses. If you change these variables these actions do not change old adressed objects. The last snippet: now str1 points to "Hell", that's all. Nobody remember that it was referred to "Hello" (both "Hello" and "Hell" chars are placed in read-only memory).

Don't mix up pointers to char (as usually treated as pointers to C-strings) with std::string class objects. Last ones are not pointers!

I tried this (using MS Visual C++ 2005/2008) and it managed to work--

#include <iostream>

using std::cout;
using std::cin;
using std::endl;
using std::flush;

int main(){

	char str1[] = "Hello";
	char* str2 = str1;
	str1[0] = 'M';
	cout << str2 << endl;

	cin.ignore();
	cin.get();
	return 0;
}

-- however when I tried your snippet of code--

char* str1 = "Hello";
char* str2 = str1; // What happens here

-- and used similar syntax mentioned above, I kept getting an access violation.

The only 2 conclusions I can guess are that in MSVC++ anonymous const char* strings don't really belong to the application. Either that or somehow the implementation may prevent modification for chars that are const (but I hardly believe this, though I've been surprised with MSVC++ quite a few times now so I don't want to throw that possibility out of the window @_@ ).

What compiler are you using?

-Alex

Of course, all good C++ implementations "prevent modification for chars that are const". All char literals placed in read-only address subspace. The C++ Standard forbid to modify text literals. However it's possible (but illegal) in old DOS implementations (BC++, for example): no hardware support for read-only memory in DOS.

char str1[] = "Hello";	
char* str2 = str1;

It's totally different case: ordinar char array is defined. You can modify its contents.

Yet another tip about pointers to C-string literals in C++:

const char* p1 = "Hello";
    const char* p2 = "Hello";
    cout << (p1==p2? "Yes" : "No!") << endl;

This program may print Yes or No! - both cases are correct! The second occurence of "Hello" literal may denote the same or different memory address. It's an implementation-defined issue.

commented: Thanks for the info! =) +5

Well see the above code

int a = 10;
int* p1 = &a;
int* p2 = p1; // Copies the address right?

But what happens here?

char* str1 = "Hello";
char* str2 = str1; // What happens here

I tried this code and when I modified str1, str2 remain unchanged why?

One more thing, if I initialize something else to str1 i.e.

char* str1 = "Hello";
str1 = "Hell";

What happens here? Is the "Hello" string deleted and space for new string is accommodated?

Hey There !

Trust Me Manzoor It Confused Me The Similar Way The First Time I Encountered The Same Problem. If I Try N Recall It Probably Took Me More That 2 Weeks To Understand The Concept Clearly.

See, We Know Pointer Are Just Like Normal Variables, Right?.
Yup, They Are Simply Variable, The Only Thing Is That They Store Address As Their Values.

Since, Pointers Store Addresses, We Can Say That "Pointers Point To Specific Places In Memory Isn't? "... Yeah, We Do.

Now While Doing Such As:

int* p1 = &X;
int* p2 = p1;

What's Simply Happening Here Is That You Are Defining Another Variable Which Points To The Same Address In The Memory Or Same Place In The Memory.

So Far So Good.

Now When We Say:

char* x = "hello";
char* y = x;

Obviously, Entirely Different Thing Is Happeneng Here.

Recall, "STRING ITSELF IS AN ARRAY OF CHARACTERS" ....
Therefore, when Ya Say

char*y = a;

y Stores The Address Of The First Element Of The Character Array.

Still Not Clear Contact Me At: SNIP

REGARDS:
MUJASH

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.