Solved

Another casting question

Posted on 2002-04-24
3
302 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
3 Comments
 
LVL 6

Accepted Solution

by:
Triskelion earned 100 total points
Comment Utility
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
Comment Utility
Don't you just hate when other people are right ?

:)

Thanks
/CrypToniC
0
 
LVL 5

Expert Comment

by:nebeker
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

763 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now