Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Another casting question

Posted on 2002-04-24
3
Medium Priority
?
311 Views
Last Modified: 2010-04-15
Hi

One of my fellow co-workers said that double-casting
has no effect
such that code like
uint32 *value
value = (uint32*)((uint8 *)(some memory address));
is equivalent to
value = (uint32*)(some memory address);

is he correct ?

I would expect the result to be double casted

Consider memory looking like
01 02 03 04
I would then expect this
value = (uint32*)(some memory address);
to contain "04 03 02 01" (when printed)

and I would expect this
value = (uint32*)((uint8 *)(some memory address));
to contain "01 00 00 00" (when printed)

Comments ?

0
Comment
Question by:CrypToniC
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 6

Accepted Solution

by:
Triskelion earned 400 total points
ID: 6965639
When you're casting pointers, the effect (with immediate use) does nothing.  It may make the compiler happy depending on your warning level.

If you're using direct data types (not pointers) such as casting a long to a short then back to a long, there will be truncation.
0
 
LVL 1

Author Comment

by:CrypToniC
ID: 6965656
Don't you just hate when other people are right ?

:)

Thanks
/CrypToniC
0
 
LVL 5

Expert Comment

by:nebeker
ID: 6965734
It depends on what you're casting.  In the example you've given, the cast really has no effect, because you're casting from one pointer type (uint8) to another pointer type (uint32).  All pointers are the same size, so there is really nothing to convert...

In your example, your casts are NOT dereferencing the memory address, so you aren't doing anything to change the value.

If you're casting between different types, then the double casting will most definitely have an effect.

If you wanted the above code to show the effects of the double cast, you would need to cast the _contents_ of the memory address, not the address itself, and to do that, you need to dereference the pointers...

For example:

uint32 *value;
uint32 x = 300;   /* for 'some memory address' */

If you do this:

value = (uint32*) (uint8 *) x;

then (*value) will be 300.

However, if you dereference the pointer, it will be converted to the type represented by that pointer.  For example:

value = (uint32*) * (uint8*) x;

then (*value) will be 44 (i.e. the lower 8 bits)...

Basically, if you have multiple casts, they will be evaluated one at a time, from right to left, causing a conversion each time.  So if you have the statement:

double x = (double)(float)(int)(short)(char) 0x1234567890;

the value "0x1234567890" will be truncated to the size of a char, then expanded to the size of a short, then an int, converted to a float, then to a double.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question