I have a few questions about c strings, string literals, c++ string objects and how pointers and constantness all come together.
I am wondering why a c string can be set like this but can't be changed dynamically with an assignment operator like this.

char str [] = "string1";
str = "string2";

But you can use cin to extract different strings to the char array?
From my reading I think I have gotten my answer but I'm not entirely sure. I'm thinking that since a c string array is a constant pointer to a memory address; by using the assignment operator you are effectively making the constant pointer point to a different block of memory. However with the extraction operator like cin or getline you are not changing the memory location being pointed to you are just changing the value contained in that block of memory, which would be legal since it is a constant pointer and not a constant pointer to a constant char. I just want to know if my reasoning is correct or if I'm totally off track.

Thx

Recommended Answers

All 2 Replies

The first one is initialisation, the second one is assignment.

Arrays cannot be assigned in either C or C++.

You also need to get out of the "arrays are pointers" mode of thinking.
http://c-faq.com/aryptr/index.html

commented: He helped me by not only answering my question but making me think about it in a different way. Thus bringing about more questions but that's a good thing. Thx again. +1

Alright, I don't mind doing the reading, thx for the link, hopefully it will clear up any questions I have. I have read through a bit of it so far it seems to be stepping towards the general direction of a lot of questions I have. Thx again, and if I have any questions on the page you sent me too, I might bug ya with private tells :)

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.