c++ question

Russel Caldwell caldr704 at gmail.com
Fri Sep 22 18:44:27 MDT 2006


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?

On 9/22/06, Byron Clark <byron at theclarkfamily.name> wrote:
>
> Russel Caldwell wrote:
> > When I do cout << &foo[0], I get "Hello". Why is that?
>
> Because cout always interprets a char * as a directive to print the NULL
> terminated string that the argument points to.
>
> If you want to see the address of foo (which is also the address of the
> first element of foo) you'll want to use printf("%p", foo).
>
> /*
> PLUG: http://plug.org, #utah on irc.freenode.net
> Unsubscribe: http://plug.org/mailman/options/plug
> Don't fear the penguin.
> */
>



More information about the PLUG mailing list