Solved

type casting

Posted on 2004-04-08
4
682 Views
Last Modified: 2013-12-03


i don't understand internally what happen when type cast occur.
0
Comment
Question by:Rajes
[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
  • 2
4 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 10781636
Hi Rajes,

When a typecast occurs , the system treats one type of data as the casted type ..

e.g. if you say char a='A'; ... then memory location denoted by a holds a bit pattern corresponding to character 'A'

when you cast it to some other type, say int ... the same bit pattern will be interpreted as an int and not as a character ...

Sunnycoder
0
 
LVL 1

Author Comment

by:Rajes
ID: 10781673

thanks for your reply.then please tell me typecasting as the basic from pointer manipulation.
0
 
LVL 11

Expert Comment

by:avizit
ID: 10781786
from a pointer point of view.

given any pointer ptr , ++ptr will increment the ptr by sizeof the object pointed by the pointer

if you cast it to another type , ++ptr will increment by the size of the casted type

and which in some cases can lead to some not so easily debuggable code.


--Abhijit


0
 
LVL 45

Accepted Solution

by:
sunnycoder earned 50 total points
ID: 10781822
int * p;

given this declaration, your compiler expects that p will hold address of a pointer ... now if you declare a struct

struct my_struct
{
     int a;
}my;

and try the statement p = &my; - compiler will generate an error because compiler expects p to hold address for an int and not some struct ... Now if you really want to hold address of "my" in p, then you need a typecast

p = (int *)(&my);  .... this will force the compiler to treat the address of my as address of an int ... this is just a way of reassuring the compiler that you know what you are doing (since the cast is explicit) and the assignment is not in error.

While typecasting has its own uses, the implications of abusing typecasting are many and most likely you will get a hard to debug and hard to maintain code ... Pointer artihmetic and data dereferencing are the most common victims
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
why debugging a macro i s difficult 10 47
sameEnds challenge 3 197
IIS Log files on Exchange 2013 server 6 347
Why  my code (program) build with old compiler? 11 151
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…
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 recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

710 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