Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.2 9/18/84; site lsuc.UUCP Path: utzoo!utcs!mnetor!lsuc!msb From: msb@lsuc.UUCP (Mark Brader) Newsgroups: net.lang.c Subject: Re: type cast in initializer Message-ID: <1100@lsuc.UUCP> Date: Sat, 8-Feb-86 00:22:46 EST Article-I.D.: lsuc.1100 Posted: Sat Feb 8 00:22:46 1986 Date-Received: Sat, 8-Feb-86 06:23:48 EST References: <302@hsi.UUCP> <1450@bbncc5.UUCP> <232@hadron.UUCP> Reply-To: msb@lsuc.UUCP (Mark Brader) Organization: Law Society of Upper Canada, Toronto Lines: 36 Summary: Reminder of what casting means In regard to... > > > int x = 0; > > > char *ptr = (char *) &x; > > In any case, I'm not sure why you'd want to do what your example does, > >since the cast here is a no-op. Joseph S. D. Yao responds... > Two reasons are immediately obvious... and goes on to give three reasons. Two are excellent, but the other is wrong: > ... on some computer > architectures, e.g. one with type-tagged pointers, the cast may > [ NOTE: n o t "will", just "may" ] not be a no-op. If this is not a slip, it stems from a misconception of casting which is a Frequently Repeated Error in this newsgroup. (If I remember what somebody posted a while ago, the error found its way into a DECUS C manual, which accounts for its widespreadness.) The cast specifies a conversion. If TTT and UUU are types, and we have: TTT t; UUU u; /* ..... */ t = u; then at that point the expressions t and (TTT)u are interchangeable. In the case of type-tagged pointers, the cast specifies that the pointer tagged "int" is to be converted to one tagged "char" and stored in the variable, but so would the initialization or assignment without the cast, if the compiler accepts it at all. Mark Brader [ The current ANSI draft C standard states: Preceding an expression by a parenthesized type name c o n v e r t s t h e v a l u e [ emphasis mine] of the expression to the named type. This construction is called a cast.]