Solved

type casting

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
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…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

867 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

20 Experts available now in Live!

Get 1:1 Help Now