Greetings,
Why does this code display ASCII values instead of HEX values?
char A[2][3];
for( i=0;i<2;i++)
for(int j=0;j<3;j++)
cout<<&A[i][j]<<endl;
I am using VC++ 6.0. However, if I change the initialization to "int
A[2][3]" instead of "char A[2][3]", then my compiler correctly
displays hex values. What is the reason for this discrepancy? Any one
who can throw some light on this issue?
Thank you in advance.
-KK
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
kewlkarun
|
11/14/2004 10:42:22 AM |
|
"KK" <kewlkarun@yahoo.com> schrieb im Newsbeitrag
news:c8fd5039.0411131407.42d14367@posting.google.com...
> Greetings,
> Why does this code display ASCII values instead of HEX values?
>
> char A[2][3];
> for( i=0;i<2;i++)
> for(int j=0;j<3;j++)
> cout<<&A[i][j]<<endl;
>
> I am using VC++ 6.0. However, if I change the initialization to "int
> A[2][3]" instead of "char A[2][3]", then my compiler correctly
> displays hex values. What is the reason for this discrepancy? Any one
> who can throw some light on this issue?
> Thank you in advance.
&A[i][j] yields a char*. In C++, there is an overload of operator<< to
output char const *, which is interpreted as C-style string.
When A is a multidimensional array of ints, &A[i][j] yields an int*. The
called overload of operator<< is the one taking a void* as second argument.
This overload prints the value of the pointer in hex.
BTW, your program yield undefined behaviour. What I explained is what seems
to happen on your platform. There is no guarantee the behaviour will be
reproducable by any means.
Thomas
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
Thomas
|
11/14/2004 8:35:16 PM
|
|
On 14 Nov 2004 05:42:22 -0500, KK wrote:
> Why does this code display ASCII values instead of HEX values?
'char*'s are strings in C++. 'void*'s aren't.
> char A[2][3];
> for( i=0;i<2;i++)
> for(int j=0;j<3;j++)
> cout<<&A[i][j]<<endl;
So this will work.
#include <iostream>
int main() {
using namespace std;
char a[2][3];
for(int i = 0; i < 2; i++)
for(int j=0;j<3;j++)
cout << static_cast<void*>(&a[i][j]) << endl;
}
--
Jeff
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
Jeff
|
11/15/2004 11:43:23 AM
|
|
KK wrote:
> Greetings,
> Why does this code display ASCII values instead of HEX values?
>
> char A[2][3];
> for( i=0;i<2;i++)
> for(int j=0;j<3;j++)
> cout<<&A[i][j]<<endl;
>
> I am using VC++ 6.0. However, if I change the initialization to "int
> A[2][3]" instead of "char A[2][3]", then my compiler correctly
> displays hex values. What is the reason for this discrepancy? Any one
> who can throw some light on this issue?
> Thank you in advance.
A char* has to be output as a string, because it's used for things like:
cout << "Hello, world\n";
;-)
Fact is that ostream provides (among others) two overloads: one that
takes a const char* and one that takes a const void*. The former outputs
a text string, the latter outputs some implementation-specific
representation of the pointer value (in your case an hex number).
For char* the first overload is chosen because it's an exact match
(apart from constness). For int* there's no exact match, but there's an
implicitly conversion to void*, so the second is chosen.
In order to output a char* as a pointer and not as a string, you can
simply convert it explicitly to void*, as in:
char A[2][3];
for(i = 0; i < 2; i++)
for(int j = 0; j < 3; j++)
cout << static_cast<void*>(&A[i][j]) << endl;
HTH,
Alberto
PS: please notice that using whitespaces correctly may greatly improve
the readability of you code, with no impact on performances ;-)
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
Alberto
|
11/15/2004 11:46:21 AM
|
|
KK wrote:
> Greetings,
> Why does this code display ASCII values instead of HEX values?
>
> char A[2][3];
> for( i=0;i<2;i++)
> for(int j=0;j<3;j++)
> cout<<&A[i][j]<<endl;
>
> I am using VC++ 6.0. However, if I change the initialization to "int
> A[2][3]" instead of "char A[2][3]", then my compiler correctly
> displays hex values. What is the reason for this discrepancy? Any one
> who can throw some light on this issue?
> Thank you in advance.
> -KK
>
the ostream interface has two operator<< for pointers. One takes as
parameter const void* and prints any pointer as an hex value.
The other takes const char* as parameter and is specially devoted to
print c-strings. Your expression "&A[i][j]" has type "char*" and is
therefore taken care by the second operator<< described.
If you want to print the address of your chars you have to do a cast:
cout << (void*)&A[i][j]<<endl;
HTH
Gianluca
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
dishsi
|
11/15/2004 11:49:02 AM
|
|
KK wrote:
> Why does this code display ASCII values instead of HEX values?
For the same reason why
std::cout << "Hello world!";
displays "ASCII values" (I suppose you mean the '\0'-terminated
character string the pointer passed to the << operator points to
- it doesn't matter if the implementation uses ASCII, EBCDIC or something
else for representing characters) rather than "HEX values" (an
implementation-defined representation of the pointer itself -
typically the memory address being the machine level equivalent
of the pointer value) - I guess you would be very surprised as well
as unhappy to see something like '0x443000' instead of 'Hello world'
on above line in your program!
> char A[2][3];
> for( i=0;i<2;i++)
> for(int j=0;j<3;j++)
> cout<<&A[i][j]<<endl;
>
> I am using VC++ 6.0. However, if I change the initialization to "int
> A[2][3]" instead of "char A[2][3]", then my compiler correctly
> displays hex values. What is the reason for this discrepancy?
When outputting an 'int*', the member function
std::ostream& std::ostream::operator<<(void const*)
is called, while when outputting a 'char*', it is the function
std::ostream& operator<<(std::ostream& out, char const* s)
[Note: these functions are actually template instantiations].
This is simply due to the fact how function overload resolution
in C++ works.
If you want to force displaying pointer values, all you need to
do is to cast:
std::cout << static_cast<void const*>(&A[i][j]) << std::endl;
Falk
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
ISO
|
11/15/2004 11:49:46 AM
|
|
In article <c8fd5039.0411131407.42d14367@posting.google.com>, KK
<kewlkarun@yahoo.com> writes
>Greetings,
> Why does this code display ASCII values instead of HEX values?
>
>char A[2][3];
>for( i=0;i<2;i++)
> for(int j=0;j<3;j++)
> cout<<&A[i][j]<<endl;
>
>I am using VC++ 6.0. However, if I change the initialization to "int
>A[2][3]" instead of "char A[2][3]", then my compiler correctly
>displays hex values. What is the reason for this discrepancy? Any one
>who can throw some light on this issue?
Maybe I have missed something but ISTM that the responses so far just do
not pin down the problem.
The std::ostream type of which std::cout is an instance provides special
handling for both char and char*. It needs to so that we can get
expected output for characters. Remember that operator << is overloaded
for ostream so that it does the correct thing for each of the
fundamental types as well as for a couple of special cases and some
Standard Library types.
One of the assumptions is that a char* will actually point to a
nul-terminated array of char. As this is not the case in your code, you
will almost always have undefined behaviour.
The general rule is that if you want the actual address that the value
of a char* represents you must cast the value to void*. This is not
unreasonable because programs rarely want to output the address of a
char and (when dealing with legacy code) often want to output a
nul-terminated string starting at a given address.
Note that there are many other places in which C++ assumes that a char*
value is to be used as a char[].
--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
Francis
|
11/15/2004 9:40:41 PM
|
|
kewlkarun@yahoo.com (KK) wrote in message
news:<c8fd5039.0411131407.42d14367@posting.google.com>...
> Greetings,
> Why does this code display ASCII values instead of HEX values?
>
> char A[2][3];
> for( i=0;i<2;i++)
> for(int j=0;j<3;j++)
> cout<<&A[i][j]<<endl;
>
> I am using VC++ 6.0. However, if I change the initialization to "int
> A[2][3]" instead of "char A[2][3]", then my compiler correctly
> displays hex values. What is the reason for this discrepancy? Any one
> who can throw some light on this issue?
[excess quoting deleted. --mod]
There are two overloads for <<in this case one takes a char* and one a
void*. The char* prints out in ascii, and the void* in hex. the int
array, which you are sending the address of, will user the void*
overload and be printed as hex
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
sgerchick
|
11/15/2004 11:43:56 PM
|
|
|
7 Replies
449 Views
(page loaded in 0.272 seconds)
Similiar Articles: Why do pointer values display as ASCII characters instead of HEX ...Greetings, Why does this code display ASCII values instead of HEX values? char A[2][3]; for( i=0;i<2;i++) for(int j=0;j<3;j++) ... Problem with Ascii code to hex code - comp.soft-sys.matlab ...Why do pointer values display as ASCII characters instead of HEX ... Greetings, Why does this code display ASCII values instead of HEX values? ... but ISTM that the ... Hex to ascii - comp.lang.asm.x86Why do pointer values display as ASCII characters instead of HEX ... Greetings, Why does this code display ASCII values instead of HEX values? char A[2][3]; for( i=0;i<2 ... display special characters - comp.soft-sys.matlabWhy do pointer values display as ASCII characters instead of HEX ... The std::ostream type of which std::cout is an instance provides special ... Some free VBA-stuff ... why and when do we need to use static redistribution ? - comp.dcom ...Why do pointer values display as ASCII characters instead of HEX ..... pointer values, all you need to do is to cast: std::cout << static_cast<void ... Down-cast a polymorphic pointer? - comp.lang.fortranWhy do pointer values display as ASCII characters instead of HEX ... If you want to force displaying pointer values, all you need to do is to cast: std::cout << static ... Forcing numbers to show with 2 decimal places for money types ...Why do pointer values display as ASCII characters instead of HEX ... char A[2][3]; for( i=0;i<2;i++) for(int j=0;j ... If you want to force displaying pointer values, all ... Can you explicitly call an implicitly defined operator ...Why do pointer values display as ASCII characters instead of HEX ... In C++, there is an overload of operator<< to ... as a pointer and not as a string, you can simply ... How to overload subsref correctly? - comp.soft-sys.matlab ...Why do pointer values display as ASCII characters instead of HEX ... How to overload subsref correctly? - comp.soft-sys.matlab ... Why do pointer values display as ASCII ... unicode display of common characters - comp.emacsWhy do pointer values display as ASCII characters instead of HEX ... unicode display of common characters - comp.emacs... instead in the html character ... display of ... Why do pointer values display as ASCII characters instead of HEX ...Greetings, Why does this code display ASCII values instead of HEX values? char A[2][3]; for( i=0;i<2;i++) for(int j=0;j<3;j++) ... Convert ASCII values from hex to characters (C# Code) • GeekpediaConvert ASCII values from hex to characters. This C# code takes in a list of ASCII values (hexadecimal) and shows the actual characters behind, thus converting hex values ... 7/24/2012 7:48:01 AM
|