Array of char

Hi,

I'm trying to understand the difference between the following two declarations:

1. char myStr[] = "text";
and
2. char* myStr = "text";


Code example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

using namespace std;

int main()
{
   // This works fine:
   char myStr[] = "text";
   myStr[0] = 'n';
   cout << myStr << endl;

   // This does not work:
   char* myStr2 = "text";
   myStr2[0] = 'n';         // This line results in a runtime error!!!
   cout << myStr2 << endl;

   return 0;
}


I can't understand why the second part doesn't work. There is no complaint from the compiler (Visual Studio 2008 Express Edition). For what I have understood there is no difference between the two declarations in the above example. I guess I have missed some fundamental part of how char arrays work. Can someone explain to me what the difference is between the two declarations?
Last edited on
There are differences between both declarations.

When you declare char myStr[] you are declaring an array of chars (which is accessible to be both read and written), and this array is initialized to some sequence of characters (ie, the value of the literal "text" is copied to the elements in this array).

While when you declare char * myStr2, you are declaring a pointer that points directly to some constant literal - not a copy, and these can only be read.

The compiler does not complain because in both cases myStr is a pointer to a char, so myStr[0] is valid in both cases. What is different is the thing they point to.
Last edited on
then how can we modify the element pointed by the char pointer?
(just like the second part)
Thanks for your answer Martin,

Okey, so there is some rule in C++ that states that a char* initialized by a string literal is a constant and can't later be changed but an array of chars initialized by a string literal is not regarded as a constant and hence allowed to be changed.

But there got to be more to it because in some ways char-pointers (char*) seem to be treated different than for example int-pointers (int*).

I explain what I mean by an example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int* pInt = new int(5);
cout <<  pInt << endl;	// -> 00385B70
cout << *pInt << endl;	// -> 5
cout << &pInt << endl;	// -> 0012FF60

char* pChar = new char('t');
cout <<  pChar << endl;	// -> t####|||ยง (one 't' followed by rubbish)
cout << *pChar << endl;	// -> t
cout << &pChar << endl;	// -> 0012FF54

char* pStr = "text";
cout <<  pStr << endl;	// -> text
cout << *pStr << endl;	// -> t 
cout << &pStr << endl;	// -> 0012FF48 


Particularly printing the value of the variable yields different output depending on if it's a pointer-to-int or if it's a pointer-to-char. pInt contains the address of the int it's pointing to which is how pointer variables are supposed to work. pChar and pStr do not contain addresses (if you print the values using cout). Instead they contain character literals and cout print them as a string.
Is there some logic in this behavior? I mean, why should the value of a pointer-to-char variable be treated in any other way than the value of a pointer-to-int or a pointer-to-float?
Last edited on
its because the << operator is overloaded for the type char* in order to print all the string. It will print all characters from the one pointed by pChar until it finds a '\0' character, i.e. a character that means end of string.

So if you do this :

char hello[8] = "HelloW";

you are storing all characters from "HelloW" in an array of char plus a '\0' char which is added by the compilator (be cautious to have enough memory allocated for it !)

then doing :

cout << hello;

will print "HelloW"

Hope this helps :)
--- had a problem when submitting response... sry
Last edited on
Thanks ness,

So if I get you right, the reason that the value of char* variable is printed as a string is that << is constructed to do just that for a char*. In other words it has nothing to do with different interpretations of char* and int* variables.
Is it correct to say that all kinds of C++ pointer variables contains an address? If you want to look at the value of pointer variable (i.e. an address) you just have to choose a method that shows you the value. And for char* that is not the << operator. Correct?
In that case: Which is the correct method for char* to do that?
An option would be to type-cast it to some other pointer type for which "cout<<" shows the address:
 
cout << (void*) mycharpointer;
Yes Martin, that did the trick. Thanks again for your input. After fiddling around a bit with char-pointers and char-arrays I think I'm starting to getting the hang of it. It's definitely not trivial and I guess it takes a lot of practise to really understand it.

Anyway, thanks again all for your input. Maybe I some day will be a real C++ programmer myself :-)
Last edited on
Topic archived. No new replies allowed.