473,396 Members | 2,011 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

questions on basic endianism program

#include <iostream>
using namespace std;

int main()
{
int testNum;
char *ptr;

testNum = 1;
ptr = (char*)(&testNum);

cout << (*ptr);
cin.get();
}

Above is a program to test the endianism of a processor. Since most
PC's are little endian, the answer displayed is usually 1 on a P.C. In
my case (Windows XP, Bloodshed Dev C++) I don't get a 1 but a strange
character which looks a bit like a zero with a smiley inside it.
However, I know that this is essentially right because when I replace
(*ptr) by (*ptr) + 1, I get the expected value of 2. It is only the 1
digit that looks strange. Does anyone know why I get this strange
character? (Or maybe this is OT because it's not a c++ phenomenon.)

The second question is more c++-related.

I replaced ptr = (char*)(&testNum); by

ptr = static_cast<char*>(&testNum); and got an invalid type conversion.
Why? What is the problem with my line? I thought I was just
translating from C to C++.

Apparently static_cast can't cast from type int* to type char*. What's
the general rule about when you can static_cast and when you can't?

Thanks for your help.

Paul Epstein

Sep 3 '06 #1
9 1945
<pa**********@att.netschrieb im Newsbeitrag
news:11**********************@p79g2000cwp.googlegr oups.com...
#include <iostream>
using namespace std;

int main()
{
int testNum;
char *ptr;

testNum = 1;
ptr = (char*)(&testNum);

cout << (*ptr);
cin.get();
}

Above is a program to test the endianism of a processor. Since most
PC's are little endian, the answer displayed is usually 1 on a P.C. In
my case (Windows XP, Bloodshed Dev C++) I don't get a 1 but a strange
character which looks a bit like a zero with a smiley inside it.
However, I know that this is essentially right because when I replace
(*ptr) by (*ptr) + 1, I get the expected value of 2. It is only the 1
digit that looks strange. Does anyone know why I get this strange
character? (Or maybe this is OT because it's not a c++ phenomenon.)
The one in "testNum = 1" is not a digit. It is an integral number. '1' would
be a digit and that would be represented as the number 49, at least on a
Windows box. What your program prints is the character with the value 1, and
that might look like a face sticking on some machines. To print a number,
you have to tell your machine to do so:

cout << static_cast<int>(*ptr);
The second question is more c++-related.

I replaced ptr = (char*)(&testNum); by

ptr = static_cast<char*>(&testNum); and got an invalid type conversion.
Why? What is the problem with my line? I thought I was just
translating from C to C++.
A static_cast can convert between related types like ints and chars or
pointers to base classes and pointers to derived classes, but not between
unrelated types like pointers to int and pointers to char. Basically you can
use a static_cast to convert from a type T1 to another type T2 if there is
an implicit or user defined conversion to convert from T2 to T1, or when you
explicitly ask for an implicit conversion in situations where the language
does not require one. As in the example above, the compiler knows how to
print a char, so there's no need to use an implicit conversion, but if you
want the it to print the character's value, you have to say so, explicitly
casting the char to an int.
Apparently static_cast can't cast from type int* to type char*. What's
the general rule about when you can static_cast and when you can't?
If you have to cast, try a static_cast first. If the compiler complains,
think about what you want to do and once you are sure you know what you are
doing, use reinterpret_cast.

HTH
Heinz

Sep 3 '06 #2
Hi,

--

<pa**********@att.netwrote in message
news:11**********************@p79g2000cwp.googlegr oups.com...
#include <iostream>
using namespace std;

int main()
{
int testNum;
char *ptr;

testNum = 1;
ptr = (char*)(&testNum);

cout << (*ptr);
cin.get();
}

Above is a program to test the endianism of a processor. Since most
PC's are little endian, the answer displayed is usually 1 on a P.C. In
my case (Windows XP, Bloodshed Dev C++) I don't get a 1 but a strange
character which looks a bit like a zero with a smiley inside it.
However, I know that this is essentially right because when I replace
(*ptr) by (*ptr) + 1, I get the expected value of 2. It is only the 1
Because it is a pointer to character it will display whatever character 1 is
not the character for one which is I believe somehigng like 49
(if this doesn't make send make it testNum = 49 or lookup the ascii code
for '1')
digit that looks strange. Does anyone know why I get this strange
character? (Or maybe this is OT because it's not a c++ phenomenon.)

The second question is more c++-related.

I replaced ptr = (char*)(&testNum); by

ptr = static_cast<char*>(&testNum); and got an invalid type conversion.
Why? What is the problem with my line? I thought I was just
translating from C to C++.
I think that should be reinterpret_cast<char*>( )

since you are casting a pointer. With static_cast the compiler converts one
type in the other and has to know how to do that.
Apparently static_cast can't cast from type int* to type char*. What's
the general rule about when you can static_cast and when you can't?

Thanks for your help.

Paul Epstein
Regards, Ron AF Greve

http://moonlit.xs4all.nl
Sep 3 '06 #3
"Heinz Ozwirk" <ho*****@arcor.dewrote in message
news:44**********************@newsspool1.arcor-online.net...
<pa**********@att.netschrieb im Newsbeitrag
news:11**********************@p79g2000cwp.googlegr oups.com...
>#include <iostream>
using namespace std;

int main()
{
int testNum;
char *ptr;

testNum = 1;
ptr = (char*)(&testNum);

cout << (*ptr);
cin.get();
}

Above is a program to test the endianism of a processor. Since most
PC's are little endian, the answer displayed is usually 1 on a P.C. In
my case (Windows XP, Bloodshed Dev C++) I don't get a 1 but a strange
character which looks a bit like a zero with a smiley inside it.
However, I know that this is essentially right because when I replace
(*ptr) by (*ptr) + 1, I get the expected value of 2. It is only the 1
digit that looks strange. Does anyone know why I get this strange
character? (Or maybe this is OT because it's not a c++ phenomenon.)

The one in "testNum = 1" is not a digit. It is an integral number. '1'
would be a digit and that would be represented as the number 49, at least
on a Windows box. What your program prints is the character with the value
1, and that might look like a face sticking on some machines. To print a
number, you have to tell your machine to do so:

cout << static_cast<int>(*ptr);
That would kinda defeat the purpose of the excersize though. Taking the
address of an int and displaying it as an int isn't going to tell him
anything about endianism. What you can do, however, is display each byte
value.

int testNum;
char *ptr;

testNum = 1;
ptr = (char*)(&testNum);

for ( int i = 0; i < sizeof int; ++i )
std::cout << static_cast<int>(ptr[i]) << " ";
std::cout << std::endl;

This would display the value of the first byte, then the second, the third
and the forth.
Sep 3 '06 #4
"Jim Langston" <ta*******@rocketmail.comschrieb im Newsbeitrag
news:PU**************@newsfe05.lga...
"Heinz Ozwirk" <ho*****@arcor.dewrote in message
news:44**********************@newsspool1.arcor-online.net...
><pa**********@att.netschrieb im Newsbeitrag
news:11**********************@p79g2000cwp.googleg roups.com...
>> char *ptr;
....
> cout << static_cast<int>(*ptr);

That would kinda defeat the purpose of the excersize though. Taking the
address of an int and displaying it as an int isn't going to tell him
anything about endianism.
That would be correct if ptr were a pointer to an int. But it is a pointer
to a char (or the OP wouldn't have ssen those silly faces on his screen).
The expression you are mis-interpreting first fetches the content of some
char variable (*ptr) and then converts it to an int (static_cast) and that
does provide some information about the machine's endiness. Your objection
would be valid only if I'd have written *static_cast<int*>(ptr) instead, but
that is something completly different.

Heinz

Sep 3 '06 #5

"Heinz Ozwirk" <ho*****@arcor.dewrote in message
news:44***********************@newsspool4.arcor-online.net...
"Jim Langston" <ta*******@rocketmail.comschrieb im Newsbeitrag
news:PU**************@newsfe05.lga...
>"Heinz Ozwirk" <ho*****@arcor.dewrote in message
news:44**********************@newsspool1.arcor-online.net...
>><pa**********@att.netschrieb im Newsbeitrag
news:11**********************@p79g2000cwp.google groups.com...
char *ptr;
...
>> cout << static_cast<int>(*ptr);

That would kinda defeat the purpose of the excersize though. Taking the
address of an int and displaying it as an int isn't going to tell him
anything about endianism.

That would be correct if ptr were a pointer to an int. But it is a pointer
to a char (or the OP wouldn't have ssen those silly faces on his screen).
The expression you are mis-interpreting first fetches the content of some
char variable (*ptr) and then converts it to an int (static_cast) and that
does provide some information about the machine's endiness. Your objection
would be valid only if I'd have written *static_cast<int*>(ptr) instead,
but that is something completly different.
Hmm.. same as
std::cout << static_cast<int>(ptr[0]);

Yes, I missed that.
Sep 3 '06 #6

Heinz Ozwirk wrote:
....
>
A static_cast can convert between related types like ints and chars or
pointers to base classes and pointers to derived classes, but not between
unrelated types like pointers to int and pointers to char. Basically you can
use a static_cast to convert from a type T1 to another type T2 if there is
an implicit or user defined conversion to convert from T2 to T1, or when you
explicitly ask for an implicit conversion in situations where the language
does not require one. As in the example above, the compiler knows how to
print a char, so there's no need to use an implicit conversion, but if you
want the it to print the character's value, you have to say so, explicitly
casting the char to an int.
....

But, in that case, why is it possible to cast from int* to char* using
(char*) &testNum

This shows that there are differences in functionality between the
older notation with (char*) and the newer syntax with static_cast.

However, my (possibly poor) texts gave me the impression that the
difference was only one of syntax.

Can anyone explain to me the difference between casts of the form

x = (new_type) expression; and

x = static_cast<new_type(expression);

Thank you,

Paul Epstein

Sep 3 '06 #7
<pa**********@att.netschrieb im Newsbeitrag
news:11*********************@p79g2000cwp.googlegro ups.com...
>
Heinz Ozwirk wrote:
...
>>
A static_cast can convert between related types like ints and chars or
pointers to base classes and pointers to derived classes, but not between
unrelated types like pointers to int and pointers to char. Basically you
can
use a static_cast to convert from a type T1 to another type T2 if there
is
an implicit or user defined conversion to convert from T2 to T1, or when
you
explicitly ask for an implicit conversion in situations where the
language
does not require one. As in the example above, the compiler knows how to
print a char, so there's no need to use an implicit conversion, but if
you
want the it to print the character's value, you have to say so,
explicitly
casting the char to an int.

...

But, in that case, why is it possible to cast from int* to char* using
(char*) &testNum
One of the goals of C++ has been to allow most C programs to be compiled
with a C++ compiler, so we must live with all the little problems of C.
This shows that there are differences in functionality between the
older notation with (char*) and the newer syntax with static_cast.
Whoever said there is no difference, didn't know what he is talking about.
However, my (possibly poor) texts gave me the impression that the
difference was only one of syntax.
So they are indeed poor texts. Get some decent book, perhaps "The C++
Programming Language" by Bjarne Stroustup (3rd edition, IIRC)
Can anyone explain to me the difference between casts of the form

x = (new_type) expression; and

x = static_cast<new_type(expression);
That should fill an entire chapter in a good book. But for a start, this
might help: http://msdn2.microsoft.com/en-us/library/c36yw7x9.aspx or just
search the web for "static_cast".

HTH
Heinz

Sep 3 '06 #8
pa**********@att.net wrote:
Heinz Ozwirk wrote:
...
>>
A static_cast can convert between related types like ints and chars
or pointers to base classes and pointers to derived classes, but
not
between unrelated types like pointers to int and pointers to char.
Basically you can use a static_cast to convert from a type T1 to
another type T2 if there is an implicit or user defined conversion
to convert from T2 to T1, or when you explicitly ask for an
implicit
conversion in situations where the language does not require one.
As
in the example above, the compiler knows how to print a char, so
there's no need to use an implicit conversion, but if you want the
it to print the character's value, you have to say so, explicitly
casting the char to an int.

...

But, in that case, why is it possible to cast from int* to char*
using
(char*) &testNum
It might not be!

Using the (char*) cast, you tell the compiler that the cast is ok.
"Trust me, I know what I'm doing!"
This shows that there are differences in functionality between the
older notation with (char*) and the newer syntax with static_cast.

However, my (possibly poor) texts gave me the impression that the
difference was only one of syntax.

Can anyone explain to me the difference between casts of the form

x = (new_type) expression; and
This is "Shut up and do it!".
x = static_cast<new_type(expression);
Here the compiler will tell you if doesn't work!
Bo Persson
Sep 4 '06 #9

Heinz Ozwirk wrote:
<pa**********@att.netschrieb im Newsbeitrag
news:11*********************@p79g2000cwp.googlegro ups.com...

Heinz Ozwirk wrote:
...
>
A static_cast can convert between related types like ints and chars or
pointers to base classes and pointers to derived classes, but not between
unrelated types like pointers to int and pointers to char. Basically you
can
use a static_cast to convert from a type T1 to another type T2 if there
is
an implicit or user defined conversion to convert from T2 to T1, or when
you
explicitly ask for an implicit conversion in situations where the
language
does not require one. As in the example above, the compiler knows how to
print a char, so there's no need to use an implicit conversion, but if
you
want the it to print the character's value, you have to say so,
explicitly
casting the char to an int.
...

But, in that case, why is it possible to cast from int* to char* using
(char*) &testNum

One of the goals of C++ has been to allow most C programs to be compiled
with a C++ compiler, so we must live with all the little problems of C.
This shows that there are differences in functionality between the
older notation with (char*) and the newer syntax with static_cast.

Whoever said there is no difference, didn't know what he is talking about.
However, my (possibly poor) texts gave me the impression that the
difference was only one of syntax.

So they are indeed poor texts. Get some decent book, perhaps "The C++
Programming Language" by Bjarne Stroustup (3rd edition, IIRC)
Can anyone explain to me the difference between casts of the form

x = (new_type) expression; and

x = static_cast<new_type(expression);

That should fill an entire chapter in a good book. But for a start, this
might help: http://msdn2.microsoft.com/en-us/library/c36yw7x9.aspx or just
search the web for "static_cast".

HTH
Heinz
Thanks, Heinz.

If I can focus on the literature aspect. Actually, the fault lies not
in the text but in the reader, that he misremembered. The text says no
such thing.

The text I am using is C++ Primer, 4th Ed. Is this a good book?

I do have a few concerns about it. For example, here is a suggested
coding fragment for a handler:

while (cin >item1 >item2) {
try { // code to add two Sales_Items together and generate an error if
this fails
} catch (runtime_error err){
// remind user that ISBN must match and prompt for another pair
cout << err.what()
<< "\n Try Again? Enter y or n" << endl;
char c;
cin >c;
if (cin && c == 'n')
break;
}
}

So what is my problem? Is this not stellar coding? Well, my concern
is... How about that catchy little tune that is so prevalent on TV ad
jingles -- "Throw by value, catch by reference" ??

(Rather than just say "they catch by value and not by reference", I
thought I would display the catch in full context.)

Is this code ok, or is catching by value poor practice here?

Paul Epstein

Sep 4 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

54
by: Spammay Blockay | last post by:
I've been tasked with doing technical interviews at my company, and I have generally ask a range of OO, Java, and "good programming technique" concepts. However, one of my favorite exercises I...
10
by: Heather Stovold | last post by:
Hi all... I am looking at writing a little program for my own use, and probably for a bunch of other people too, and I am trying to decide what would be the best language to use. I am a...
2
by: Fay Yocum | last post by:
BEWARE beginner questions!! I have some experience in Access but never as much as I want or need. I have decided to get in on VB.Net. I would only rate myself in Access as a...
1
by: HankG | last post by:
I use MS Access (W95 version on an XP computer) to create databases for my own use, and would like to develop a stand-alone version of one, which could be marketed to a specific group of...
4
by: AzizMandar | last post by:
C++ Event Coding Questions I have done some simple programs in C++ and read a lot of good C++ books (Including The C++ Programing Language, and C++ Primer) I am trying to understand and...
14
by: shamirza | last post by:
Question Do ActiveX DLLs made in VB still need the VB runtimes on the machine? ________________________________________ Answer In a word, Yes. Visual Basic does not support what is known...
10
by: See_Red_Run | last post by:
Hi, I am trying to figure out how to get started with PHP/MySQL. Everything I've read so far says to start with PHP first. I was expecting something like Visual Basic Express or some other type...
8
by: Krypto | last post by:
Hi, I have used Python for a couple of projects last year and I found it extremely useful. I could write two middle size projects in 2-3 months (part time). Right now I am a bit rusty and trying...
12
by: jorba101 | last post by:
I'm aware of the importance of the concept of endianism, but I don't really know its formal definition. In particular, what does mean "big-endian" when applied to network programming? i.e.,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.