Solved

type casting

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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How do I customize my Standard Colors in PowerPoint 2010? 6 50
Font install problem 9 46
Source of man or women figure in Text Box 7 106
What is atomic operation? 6 59
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…
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…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-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.

773 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