Arrays of characters

Hallo,
I know that in C++ the name of an array is automatically converted into a pointer to the first element of the array.
So, if we have the following array
char ca[] = “Hallo world!”;
I can take the first character by dereferencing ca:
cout << *ca << endl; //OK, output: H
Now, I want to take the second character, a, in two different ways:
1
2
cout << *(ca + 1) << endl;   //OK, output: a
cout << *(++ca) << endl;	  // compilation error! ++ needs l-value 

The second way doesn’t work. But, if ca is automatically converted into a pointer, why I can’t use it as an actual pointer?

Thank you.
It's just like the compiler says - you can't use ++ on a temporary.
Similarly, int x=5++; won't compile and neither will
1
2
int x;
int* p=++&x;


1
2
int f() {return 1;}
int x=++f();
Last edited on
Topic archived. No new replies allowed.