Article View: alt.msdos.programmer
Article #1210Re: typecasting preferences
From: Lawrence Kirby
Date: Thu, 18 Apr 1996 15:20
Date: Thu, 18 Apr 1996 15:20
67 lines
3268 bytes
3268 bytes
In article <4l4ldb$nkl@srvr1.engin.umich.edu>
hasdi@news-server.engin.umich.edu "HASDI RODZMANN HASHIM" writes:
>The way I understand it, type-conversion is to transform the bits of one
>variable so that it is representable as the type to be converted into. eg.
>(int)my_float. type-casting doesn't transform the bits at all, but force
>the compiler to treat that variable as the specified type... which is
>highly unportable. Why would anyone want to typecast then? Can somebody
>clarify this for me?
You've misunderstood. Casting is simply a way of forcing type conversions
explicitly - changes of representation i.e. the bits patterns or even the
number of bits occupied by the value are in general required. For example
int i;
double d;
i = 1;
x = i;
This involves an implicit conversion and the result will be that x contains
1.0 (or something as close as possible to it).
x = (double) i;
has exactly the same result. However the conversion to double is performed
explicitly by the cast instead of implicitly by the assignment. Pointer
casts can also result in a change of representation for the pointer. However
there is no effect in what is being pointed to so you can end up with a
pointer pointing to something that was created as a different type. E.g.
int x = 1;
char *p = (char *)&x;
&x is a pointer to int which is cast to a pointer to char. The representation
of the pointer may change if the system uses different representations
fort int * and char * (some systems do). If you dereference p you will
be accessing what it points to as a char although the value containted in x
is represented as an int.
>As a side note, using typedef so that you can use "myrecord" instead of
>"struct myrecord_s" has been a real major bug to me.
Why? Structure tags allow you tag the fundamental structure types. typedefs
are a general mechanism in C to create aliases for any type including
structures.
>Unfortunately, this
>is necessary to avoid shift/reduce conflicts.
You may have to define the grammar differently but I see no need for
conflicts. The main issue is that in C tags have their own namespace. That
is only possible if they can be distinguished from other types of
identifier syntactically.
>Makes me wonder how C++ parser works.
There's nothing special here however C++ doesn't give tags a separate
namespace.
--
-----------------------------------------
Lawrence Kirby | fred@genesis.demon.co.uk
Wilts, England | 70734.126@compuserve.com
-----------------------------------------
Technical Details
Message-ID:
<829840807snz@genesis.demon.co.uk>Path:
arc1-ca.pugleaf.net!mail2news.demon.co.uk!genesis.demon.co.ukReferences:
<4l020g$i9j@srvr1.engin.umich.edu> <Pine.A32.3.91.960416145209.106109C-100000@black.weeg.uiowa.edu> <4l4ldb$nkl@srvr1.engin.umich.edu>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!mail2news.demon.co.uk!genesis.demon.co.uk From: Lawrence Kirby <fred@genesis.demon.co.uk> Newsgroups: alt.msdos.programmer,comp.lang.c,comp.lang.pascal.misc Subject: Re: typecasting preferences Date: Thu, 18 Apr 96 15:20:07 GMT Organization: none Distribution: world Message-ID: <829840807snz@genesis.demon.co.uk> References: <4l020g$i9j@srvr1.engin.umich.edu> <Pine.A32.3.91.960416145209.106109C-100000@black.weeg.uiowa.edu> <4l4ldb$nkl@srvr1.engin.umich.edu> Reply-To: fred@genesis.demon.co.uk X-NNTP-Posting-Host: genesis.demon.co.uk X-Newsreader: Demon Internet Simple News v1.27 X-Mail2News-Path: genesis.demon.co.uk