0

Here is sth I find but I can't understand one address ,when I use "cout<

#include<iostream>
using namespace std;
int main()
{
    char  a[2]={'a','b'};
    char  b[3]="ab";
    cout<<&a<<endl;
    cout<<&b<<endl;
    cout<<sizeof(a)<<endl<<cout<<sizeof(b);//the result of this I am puzzled
    return 0;
}

The result is :

0x28ff2e
0x28ff10
2
0x4453c43
Almo
  • 15,538
  • 13
  • 67
  • 95
Fanl
  • 1,491
  • 2
  • 11
  • 15
  • 4
    Since you seem to be just starting out with C++ programming, I'll mention a style thing: it's easier to read your code if you put spaces around operators, like this `cout << sizeof(a) << endl << cout << sizeof(b);` :) – Almo Mar 14 '14 at 15:37

7 Answers7

5

0x28ff2e is an address of a
0x28ff10 is an address of b
2 is the size of a
0x4453c43 is an address of the result of converting cout to void* followed by sizeof(b) (See Does std::cout have a return value?)

Maybe you did want this instead:

cout << sizeof(a) << endl;
cout << sizeof(b) << endl;

Or this:

cout << sizeof(a) << endl << sizeof(b) << endl;
Community
  • 1
  • 1
mirelon
  • 4,896
  • 6
  • 40
  • 70
3

When you do this line:

cout<<sizeof(a)<<endl<<cout<<sizeof(b)

You shouldn't use cout second time. When you do, you printf address of it:

0x4453c4

or rather Does std::cout have a return value?

and then you print size of b, is the 3 on the end of this 0x4453c43

Rather you should just use this:

cout<< sizeof(a) << endl << sizeof(b) << endl;
Community
  • 1
  • 1
Ardel
  • 315
  • 2
  • 9
  • 2
    It's not (necessarily) the address of `cout`. That's `&cout`. Instead, it's `cout` converted to a `void *` via the conversion operator. – chris Mar 14 '14 at 15:41
1

You're printing the address of cout ;)

cout<<sizeof(a)<<endl<<cout<<sizeof(b)
mcopik
  • 341
  • 2
  • 6
  • 1
    The address of `cout` is `&cout`, not `cout`. The reason this works is that `cout` can be converted to a `void *`. – chris Mar 14 '14 at 15:39
  • Yeah, my mistake. I didn't notice this. – mcopik Mar 14 '14 at 15:41
  • @chris Implementations typically like to return `this` cast to `void*`, so afterall, it *is* the address of `cout` :) – jrok Mar 14 '14 at 15:42
  • @jrok, Yeah, I added "not necessarily" to my other comment since implementations like to do that for `std::ostream`. – chris Mar 14 '14 at 15:43
1

The problem is that you're streaming the cout object to itself, so it prints whatever the cout can be converted to that operator<< if overloaded for - which happens to be void* (before C++11, which you evidently aren't using).

Either break cout<<sizeof(a)<<endl<<cout<<sizeof(b); into two lines with a semicolon after endl, or remove the second cout. You should put in a final endl or '\n' too... on some systems you won't be able to read the output otherwise (as the shell prompt will return to the left-of-screen then overwrite it).

Tony Delroy
  • 102,968
  • 15
  • 177
  • 252
1

You probably don't want this bit:

cout<<sizeof(a)<<endl<<cout<<sizeof(b);
                     ^^^^^^

Older implementations of streams had an operator void*(), to allow constructs like if (cout) without allowing implicit conversions to bool and other numeric types; you are seeing the result of that, concatenated with the final value. A C++11 implementation should have an explicit operator bool() insead, and so should cause a compile error here.

Removing that gives something like:

0x28ff2e
0x28ff10
2
3 

as you would expect.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
0

"the result of this I am puzzled"

cout< < sizeof(a)< <endl << cout <<sizeof(b);
                            ^^^ The "puzzling" address comes from this

Remove it and see the result

cout output to standard output is implementation defined

P0W
  • 46,614
  • 9
  • 72
  • 119
0

You have a stray cout there. Note that you're effectively printing cout into cout here:

cout << ... << cout << sizeof(b);

cout needs some form of conversion to boolean. Before C++11, this was provided by a conversion to void*; this happens in your case, as the only way cout can be converted to something streamable. Note that there's an extra 3 after 6 hexa digits of the pointer - that's the 3 produced by sizeof(b)

Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455