Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

type casting

Posted on 2004-04-08
4
Medium Priority
?
685 Views
Last Modified: 2013-12-03


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

Technology Partners: 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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
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…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

886 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