473,508 Members | 2,365 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Could someone please explain?!

Hello,

1 unsigned char ar[] = "AB";
2 unsigned int i = 0;
3 i = *(unsigned int *)ar;

How does it convert to unsigned int from unsigned char()? After
executing this three line I am getting 16961! How does this value
actually derived?

Thanks
JK

Jan 6 '06 #1
5 1449
> 1 unsigned char ar[] = "AB";
2 unsigned int i = 0;
3 i = *(unsigned int *)ar;

How does it convert to unsigned int from unsigned char()? After
executing this three line I am getting 16961! How does this value
actually derived?


ar is the array containing "AB" with a trailing zero to terminate the
string.
It is first implicit converted to a pointer to the first character.
Then you typecast this pointer to a pointer which points to an unsigned int.
This means that the int starts at the same address as your string. If we
assume that in your implementation a char is 1 byte and an int is 4 bytes,
then the int will consists of the A, the B, the trailing zero and the byte
following that (which can have any value).
Depending upon your processor this is either with the most significant byte
at the first or last address.
An Intel processor will have it at the last address, so i will become
'A'+'B'*256+0*256*256+unspecified*256*256*256.
In ASCII 'A' is 65 and 'B' is 66 and apparently the unspecified value was
zero in this case, so you end up with 65+66*256=16961

Niels Dybdahl
Jan 6 '06 #2

ji******@gmail.com wrote:
Hello,

1 unsigned char ar[] = "AB";
2 unsigned int i = 0;
3 i = *(unsigned int *)ar;

How does it convert to unsigned int from unsigned char()? After
executing this three line I am getting 16961! How does this value
actually derived?


As an aside from Neil's explanation -

Note that almost everything is implementation and architechture
dependent here, and not in the domain of std c++.
1. Size of char, int
2. Endianness of machine
3. casting from unsigned char* to int*

note that this type of typecasting has implementation defined effects
and hence is almost always guaranteed to be non-portable. Also, avoid
C-style casts. Use reinterpret_cast in this case (and C++ style casts
in general).

Jan 6 '06 #3

<ji******@gmail.com> skrev i meddelandet
news:11*********************@o13g2000cwo.googlegro ups.com...
Hello,

1 unsigned char ar[] = "AB";
2 unsigned int i = 0;
3 i = *(unsigned int *)ar;

How does it convert to unsigned int from unsigned char()? After
executing this three line I am getting 16961! How does this value
actually derived?

Thanks
JK


Here's another view of what is happening here:

The name ar denotes an array of unsigned char. In some places, ar will
'decay' into a pointer to the first element of the array. Here the
type of the pointer would be 'unsigned char *'.

When you cast that pointer to another type, the compiler doesn't
convert it. It is YOU who say "the pointer is not a pointer to an
unsigned char, it is really a pointer to an unsigned int. Trust me, I
know what I am doing!". And the compiler is forced to trust you,
because that's the contract it works under.

If you then dereference the pointer, and it actually DOES point to an
unsigned int, you get the value of the int.

If you dereference the pointer, and it actually does NOT point to an
unsigned int, you have broken the contract with the compiler. It is
the free do do anything it feels like, perhaps show the value 16961.
Or crash, or format your harddisk. :-)
Just don't lie to the compiler.
Bo Persson
Jan 10 '06 #4
16961 in Hex is 0x4241

If you look in a ascii chart, "A" is 0x41 "B" is 0x42.

Assuming you have a 4-byte unsigned int, the other 2 bytes were 0 at the
time, though don't count on it.

-Tom

"Bo Persson" <bo*@gmb.dk> wrote in message
news:42*************@individual.net...

<ji******@gmail.com> skrev i meddelandet
news:11*********************@o13g2000cwo.googlegro ups.com...
Hello,

1 unsigned char ar[] = "AB";
2 unsigned int i = 0;
3 i = *(unsigned int *)ar;

How does it convert to unsigned int from unsigned char()? After
executing this three line I am getting 16961! How does this value
actually derived?

Thanks
JK


Here's another view of what is happening here:

The name ar denotes an array of unsigned char. In some places, ar will
'decay' into a pointer to the first element of the array. Here the type of
the pointer would be 'unsigned char *'.

When you cast that pointer to another type, the compiler doesn't convert
it. It is YOU who say "the pointer is not a pointer to an unsigned char,
it is really a pointer to an unsigned int. Trust me, I know what I am
doing!". And the compiler is forced to trust you, because that's the
contract it works under.

If you then dereference the pointer, and it actually DOES point to an
unsigned int, you get the value of the int.

If you dereference the pointer, and it actually does NOT point to an
unsigned int, you have broken the contract with the compiler. It is the
free do do anything it feels like, perhaps show the value 16961. Or crash,
or format your harddisk. :-)
Just don't lie to the compiler.
Bo Persson

Jan 10 '06 #5

"Tom Wilson" <to*@tom.tom> skrev i meddelandet
news:dq**********@gnus01.u.washington.edu...
16961 in Hex is 0x4241

If you look in a ascii chart, "A" is 0x41 "B" is 0x42.
Sure, that's an explanation for why this particular value might show
up.
Assuming you have a 4-byte unsigned int, the other 2 bytes were 0
at the time, though don't count on it.
The problem here is that formally we don't have any unsigned ints in
sight, just some unsigned chars. Then we tell the compiler that we
know better, that 'ar' really points to an unsigned char anyway.
"Honest, Gov. You can trust me. I would never lie to anyone!".

And we don't really have 4 bytes (if that is the size of an unsigned
int on this particular machine), we just have 3. So now we try to look
at the presumably first 4 bytes out of these 3. What happens? Poof?
16961? Something else?

Who knows, we have broken the contract. The compiler has the right to
sue us!
Bo Persson

-Tom

"Bo Persson" <bo*@gmb.dk> wrote in message
news:42*************@individual.net...

<ji******@gmail.com> skrev i meddelandet
news:11*********************@o13g2000cwo.googlegro ups.com...
Hello,

1 unsigned char ar[] = "AB";
2 unsigned int i = 0;
3 i = *(unsigned int *)ar;

How does it convert to unsigned int from unsigned char()? After
executing this three line I am getting 16961! How does this value
actually derived?

Thanks
JK


Here's another view of what is happening here:

The name ar denotes an array of unsigned char. In some places, ar
will 'decay' into a pointer to the first element of the array. Here
the type of the pointer would be 'unsigned char *'.

When you cast that pointer to another type, the compiler doesn't
convert it. It is YOU who say "the pointer is not a pointer to an
unsigned char, it is really a pointer to an unsigned int. Trust me,
I know what I am doing!". And the compiler is forced to trust you,
because that's the contract it works under.

If you then dereference the pointer, and it actually DOES point to
an unsigned int, you get the value of the int.

If you dereference the pointer, and it actually does NOT point to
an unsigned int, you have broken the contract with the compiler. It
is the free do do anything it feels like, perhaps show the value
16961. Or crash, or format your harddisk. :-)
Just don't lie to the compiler.
Bo Persson


Jan 10 '06 #6

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

Similar topics

0
1338
by: alt | last post by:
UNIX_TIMESTAMP() UNIX_TIMESTAMP(date) If called with no argument, returns a Unix timestamp (seconds since '1970-01-01 00:00:00' GMT) as an unsigned integer. If UNIX_TIMESTAMP() is called with a...
13
2444
by: C++fan | last post by:
The following code is for list operation. But I can not understand. Could anyone explain the code for me? /* * List definitions. */ #define LIST_HEAD(name, type) struct name { type...
21
1595
by: Gactimus | last post by:
Can anyone explain what the lines with the '*' by them do? ----------- #ifndef _COUNTER_H #define _COUNTER_H #include <iostream> using namespace std; class Counter
3
1580
by: MarcJessome | last post by:
Hi, I was wondering if someone could help me through learning C++. I've tried learning before, but I find I would work better if I had someone that could help explain a few things to me. Im using...
5
1449
by: klj_mcsd | last post by:
Let's say you set txtlastname1.visible = true Then DirectCast(Page.FindControl("txtLastName1"), TextBox).Visible = False Why when you check txtlastname1.visible it is equal to True?
2
4841
by: blueyonder | last post by:
The statament below does exactly what I want it to do but I don't understand why? In my mind the subquery produces a result set which is a subset of the handset table which the initial part of...
2
1343
by: hassruby | last post by:
Can someone pls help me with some validation that im having a few technical problems with in my program. First of all, I will explain to you a little about what my program is suppose to do. It...
8
1642
by: Bart | last post by:
Could someone explain me what is wrong with this code ? I gives me a compile error: Error 1 Use of unassigned local variable 'fileStreamObject' C:\Documents and Settings\Bart\Local...
2
9802
by: patrice.pare | last post by:
Hello, Here is a summary of my Dev Environment: I use Visual Studio 2005 Team Suite SP1 with Crystal Report XI SP1 on a Windows XP SP2 development workstation. I also use SQL Server 2000 SP4. ...
3
1387
by: Aarti | last post by:
Hi, Can some one please explain why the output of this program is 15 #include <iostream> using namespace std; class A {
0
7115
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7321
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7377
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
7489
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
5624
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,...
1
5047
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3191
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1547
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
414
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.