Pointers Concept Clarification

May 21, 2013 at 5:27am
Hi everyone!
I am a little confused while comparing char pointers to integer pointers.
Here is the problem:
consider the following statement;
char *ptr = "Hello";
char cArr[] = "Hello";
when I do cout << ptr; it prints Hello, same is the case with the statement
cout << cArr;
as ptr and cArr are pointers, they should print addresses rather than contents, but if I have an interger array i.e.
int iArr[] = {1, 2, 3};
if I cout << iArr; it displays the expected result(i.e. prints address) but pointers to character array while outputting doesn't show the address but shows the contents, Why??
Please address the issue.
Thanks in advance

Regards,
Ali
May 21, 2013 at 5:29am
The << operator is overloaded to, when printing a char*, actually acquire the string at that location and print it instead of the address. To get the address you can cast it to another pointer type.
May 21, 2013 at 5:29am
The first statement you posted is incorrect. You must have a const char*, not simply a char*.

As for your question, that is because character arrays are treated differently, so instead of printing the address it prints the "string" of characters.
Topic archived. No new replies allowed.