'cast'ing is a mechanism for telling the compiler what role a variable is playing. Since 'C' is a strongly typed language (others may disagree) the compilers tend to be picky about how we use variables. Most often the expression 'type casting' is used when talking about 'cast'ing. Meaning that we are taking a variable of one kind and using it in another way. It's been my experience that casting is most often used with pointer types. An example that comes to mind is Window messaging. The parameters to a windows message often include the 'types' LPARAM and WPARAM. Now in reality those are most often (i'll probably get corrected) simply integers. But I need to tell the compiler that I KNOW my integer value is a WPARAM or LPARAM:
The windows API defines SendMessage( HWND, MSG, WPARAM, LPARAM); if in my program i have: int rc; char *lpsz_ the_text = "Hello World"l which I wan to add to a listbox, I might call: rc = SendMessage( hWndList, LB_ADDSTRING, 0, lpsz_the_text); the compiler is going to give me an error message telling me that parameter 4 is an incorrect type! (or words to that effect, I'm on my mac and doing this from memory so can't compile this to get the exact error message) So I look at my code, and see that in fact, I'm passing the wrong 'type' ( a long pointer to a string, zero terminated and NOT an LPARAM) but I know that that is what I want to pass to get a string into a list box. So I have to 'type cast' (turn one type into another) to get the silly compiler to do what I want. So I recode the call as rc = SendMessage( hWndList, LB_ADDSTRING, 0, (LPARAM) lpsz_the_text); Most of the time I use 'cast'ing is with pointers. To do it with integral types can be very dangerous. As I see someone pointed out already with cruise control in a car example. Hope this helps Michael Comperchio [email protected] On Mar 19, 2009, at 3:50 AM, zeb_zxc wrote: > it is very difficult to understand the casting(data type > conversion)can any body help me in understanding the casting > >
