Because your program has undefined behavior. And because you ask it to
print different things.
Your invocation of printf
is illegal, and results in undefined
behavior (and is a good example of why you should never use printf
).
Your format specifier says to extract an unsigned int
from the
argument list, and output that in hexadecimal. Passing it anything but
an unsigned int
is undefined behavior. As it happens, given the way
varargs are generally implemented, if you're on a machine where
unsigned
s and pointers have the same size, you'll probably output the
value of the pointer, treating its bits as if it were an unsigned
.
Other behaviors are certainly possible, however. (If I'm not mistaken,
g++ will warn about this construct; it's also possible that on some
platforms, it will crash.)
In the case of std::cout
, you're passig it a char*
. By definition,
the char*
is treated as a '\0' string, not as a pointer (and
certainly not as an unsigned int
). And again, you have undefined
behavior, since your char*
doesn't point to a '\0' terminated string;
you never put a '\0' at the end. (This probably explains the "N???"
you see at the end of your output. But again, undefined behavior is,
well, undefined. The code could just as easily have crashed.)
Finally, you're using both printf
and std::cout
; the results are not
really specified unless you do a flush of the stream between the two.
(In practice, if you're outputting to an interactive device, the flush
should occur when you output the '\n'
character. If you redirect the
output to a file, however, you're likely to get something different.)
It's not clear what you want. If you want to output the address of
array
, it would be:
printf( "%p\n", test );
std::cout << static_cast<void*>( test ) << std::endl;
If you want to output the string you've generated, then append a '\0' to
the end of it (without overflowing the buffer), and then:
printf( "%s\n", test );
std::cout << test << std::endl;
I'm not sure what you're trying to make "hex"; there is no such thing as
a hex representation of a string, and the representation of a pointer is
implementation defined, and not required to take into account any
formatting parameters in iostream. (Typically, on most modern machines,
it will be hex. But I've worked on more than a few where it would be
octal, and at least one where it wouldn't be just a number, regardless
of the base.) If you want a hex dump of array
, you'll have to loop,
outputting each value as an unsigned
in hex:
for ( int i = 0; i < 10; ++ i ) {
printf( "%x", static_cast<unsigned char>( test[i] ) );
}
printf( "\n" );
std::cout.setf( std::ios_base::hex, std::ios::basefield );
for ( int i = 0; i < 10; ++ i ) {
std::cout << static_cast<unsigned>( static_cast<unsigned char>( test[i] ) );
}
std::cout.setf( std::ios_base::dec, std::ios::basefield );
std::cout << std::endl;
Finally: a word about the casts: plain char
may be either signed or
unsigned; if it is signed, converting it to an int
or an
unsigned
, might produce either a negative value (int
) or a very
large positive value (unsigned
). Thus, the first conversion to
unsigned char
, which guarantees a result in the range [0, UINT_MAX]
.
Second, of course, we have to convert the unsigned char
to unsigned
:
in the case of printf
, because we would otherwise have undefined
behavior, but the conversion is implicit, since passing an unsigned
char
as a vararg automatically promotes it to unsigned
; and
in the case std::cout
, because the rules are that any character
type be output as a character, not as a numerical value (and since the
type is used here in function overload resolution, and is not being
passed to a vararg or an unsigned
, there is no implicit conversion).