Domanda

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
È stato utile?

Soluzione

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;

Altri suggerimenti

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;

You're printing the address of cout ;)

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

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).

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.

"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

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)

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top