Solved

type casting

Posted on 2004-04-08
4
675 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 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

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…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

708 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

12 Experts available now in Live!

Get 1:1 Help Now