c++ question

Byron Clark byron at theclarkfamily.name
Fri Sep 22 19:42:13 MDT 2006

Russel Caldwell wrote:
> Sorry about this but this conversation has uncovered an apparent
> misunderstanding on my part about arrays. When I do the following:
>     int foo[5] = {1, 2, 3}
>     cout << &foo;       //I get an address
>     cout << foo;         //I get the same address
>     cout << &foo[0];   //I get the same address
>     cout << foo[0];     //I get the value stored in the first slot
> What this seems to be telling me is that the address of foo[0] is stored at
> the same address as the value of foo[0]. What am I missing?

Your understanding of pointers and how they work is just fine.  What's
missing is how cout (specifically ostream::operator<<) is handling them.
 In most cases 'cout << pointer' will print the pointer address.  The
exception is when pointer is a 'char *' or 'wchar_t *' in any of its
forms.  In those cases, ostream::operator<< is overloaded to place the
NULL terminated string on the output stream instead of the address
pointed to by the pointer.

Byron Clark

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 254 bytes
Desc: OpenPGP digital signature
Url : http://plug.org/pipermail/plug/attachments/20060922/baec702c/attachment.bin 

More information about the PLUG mailing list