How to character pointers work?

char * c = "Hello World"

"Hello World" is not an address in memory. How does this work.

*c='c'
Why doesn't this work and what does the value it had before signify?

c = "Cplusplus"

Does "Hello World" get replaced or is c just another address in memory now? Wouldn't "Hello World" be a memory leak then?


Don't respond to this topic "Just use string."
c will point to the first character, 'H' and then 'C'. It is a C-string because it points to an array of characters. You can dereference c with the subscript operator just like an array (because they are basically pointers too).
1
2
3
char* c = "Hello world";
for(int I=0; I < 11; ++I)//Note the number 11; you cannot find the length of "Hello world" from the pointer
   cout << c[I];


There is no memory leak. "Hello world" is stored in the stack and not the heap.
Char pointers are overloaded differently than ints so that is why when you output it displays the characters and not the address.
1
2
3
4
char *c = "Hello World"
int i = 0 , *ptr = &i;
std::cout << c << std::endl;
std::cout << ptr << std::endl;
AlitCandle wrote:
char * c = "Hello World"

"Hello World" is not an address in memory. How does this work.

"Hello World" is not an address, but c is. By the way, it should be declared:

const char * c = "Hello World";

This is because the string data (the characters) is stored in your program's read-only data segment (which is the only safe assumption you can make unless you know something specific about your compiler and OS).

You can always make the pointer point to (an)other character(s):

1
2
const char * c = "Hello World";
c = "CPlusPlus";  // c now addresses a completely different piece of read-only memory 


@Daleth
Don't hardcode the length of the string. Use the fact that all c-strings terminate with a null character.

1
2
3
const char* c = "Hello world";
for(int n=0; c[n]; ++n) 
   cout << c[n];

You could also say:
1
2
for (const char* p = c; *p; p++)
  cout << *p;


@all
Read more about how pointers, and their relationship to strings, here:
http://www.cplusplus.com/faq/sequences/strings/c-strings-and-pointers/

(Also tell me what you think, so I can improve it if I can.)

Hope this helps.
The code here is misleading:
char *str = "Hello";

It is in fact impossible to do:
1
2
3
char *str = "Hello";
str = "World"; // compile error
str[0] = 'W'; // compile error 

str is constant


The correct thing to do is:
const char *str = "hello";
Because that is what it is.

All c-strings are null terminated, thus their length can be calculated:
1
2
3
4
5
6
int length = 0;
while (str[length]) ++length;

// also
#include <cstring>
size_t length = strlen(str);  // size_t is of unsigned integral type 
Last edited on
Oh, right, thanks Duoas and Gilbit. I work with C-strings and \0 so rarely that the information is kind of rusting in my head.
Dunno what else can be improved about the article (since I am still a beginner) because it is already succinct and easy to understand.
> str = "World"; // compile error
¿what is the error message? it compiles fine over here (gcc 4.8)
@LowestOne

The character data is constant.
The pointer is not.

Therefore, str = "World"; is valid. If your compiler is telling you different then you need to figure out why.

If you want the pointer to be constant, you must place const on the other side of the asterisk:

const char * const s = "I yam what I yam what I yam."

Alas for the weirdness of syntax. Anyway, hope this helps.
And, of course, C++11 has made this illegal (about time!)

char* c = "Hello World"

You've got to use a const char*.

Andy

PS Unfortunately you can still force it with a cast, if you're mad enough.
Last edited on
Topic archived. No new replies allowed.