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

Attachment: signature.asc
Description: OpenPGP digital signature

/*
PLUG: http://plug.org, #utah on irc.freenode.net
Unsubscribe: http://plug.org/mailman/options/plug
Don't fear the penguin.
*/

Reply via email to