0

Questions regarding, well, ultimately pointers to pointers (I suspect). Please read the questions posed in the commented code:

void doodah(char* a);

int main() {

    char d[] = "message"; // one way of assigning a string.
    // char* d = "message"; // another way of assigning a string, but REM'ed out for now.
    cout << d << endl; // d appears not to be a pointer because cout outputs "message", and not an address. Why is this?
    doodah(d); // a function call.
}

void doodah(char* a) {

    cout << a << endl; // this outputs "message" - but why?! What does 'a' mean in this context?
    // cout << *a << endl; // this outputs "m" - but why?! REM'ed out for now.
}

I am utterly confused! Please help.

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
  • `cout` prints the pointer to `char` as a C string, not its memory address. –  Aug 07 '14 at 23:24
  • `void main` is illegal C++. – Rapptz Aug 07 '14 at 23:38
  • Maybe it will help to try "std::cout << " d:\t" << d << " " << static_cast(d) << std::endl;". Casting to "const void*" tells the compiler to ignore the type of d, and cout can then interpret and print it as a address. – 2785528 Aug 07 '14 at 23:47

3 Answers3

2

cout knows how to output strings when given a char *. It does not attempt to print the pointer value itself.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
0

An array is a bunch of objects next to each other somewhere in memory. The variable you've set the array to is actually secretly a pointer to the first item in that array (shhh!).

The biggest difference between char *c and char c[] is that the latter will be a const pointer, while the former is free to change. Also, C-Strings, like the one you have set there, are null terminated, meaning that the array ends in a binary 0 so things like cout will know when to stop iterating (this is also known as a one pass last).

For more information, you can read up on this question.

Community
  • 1
  • 1
scohe001
  • 15,110
  • 2
  • 31
  • 51
0

This is what the pointer a looks like in memory:

-------------------------------------------------------------
|      | 'm' | 'e' | 's' | 's' | 'a' | 'g' | 'e' | '\0'     |
|         ^                                                 |
|         |                                                 |
|        ---                                                |
|        |a|                                                |
|        ---                                                |
-------------------------------------------------------------

a is a pointer to the first element of the string. When used in the stream inserter (operator<<()) the compiler will match it with the overload that takes a stream on its left hand side and a pointer to a character on its right hand side. It will then attempt to print every character until it reaches the null byte ('\0') by evaluating characters at incremental addresses from a.

The stream prints addresses through the overload that takes a void* on its righthand side. You can cast your pointer to a void* or use the standard-provided std::addressof() function as well:

std::cout << std::addressof(a);
David G
  • 94,763
  • 41
  • 167
  • 253