Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!linus!philabs!cmcl2!harvard!seismo!vrdxhq!rlgvax!hadron!jsdy From: jsdy@hadron.UUCP (Joseph S. D. Yao) Newsgroups: net.lang.c,net.arch Subject: Re: Byte order (retitled) Message-ID: <397@hadron.UUCP> Date: Thu, 8-May-86 09:47:34 EDT Article-I.D.: hadron.397 Posted: Thu May 8 09:47:34 1986 Date-Received: Sat, 24-May-86 04:28:05 EDT References: <7046@cca.UUCP> <7158@cca.UUCP> <1104@psivax.UUCP> <2590@utcsri.UUCP> <1117@psivax.UUCP> <5056@think.ARPA> Reply-To: jsdy@hadron.UUCP (Joseph S. D. Yao) Organization: Hadron, Inc., Fairfax, VA Lines: 31 Summary: Showing why Sarima is right; inviting counter-demonstration In article <5056@think.ARPA> rose@think.UUCP (John Rose) writes: >In article <1117@psivax.UUCP> friesen@psivax.UUCP (Stanley Friesen) writes: >> And I am saying that the following *should* be portable, and >>that any implementation that it doesn't work on is brain-damaged. >> register int i; >> unsigned char bytes[ sizeof( long )]; /* lo-byte first */ >> long input; >> register char *cvptr; >> for(cvptr = (char *)&input, i = 0; i < sizeof(long); i++) >> bytes[i] = cvptr[i]; >Well, this is certainly false for all existing C's on big-endian machines. >Even if you make the pointer "(char *)&input" point to the last byte of >the datum, when you index on it, say by referencing "cvptr[1]", you get >bytes *past* the last byte of the long datum. I don't see rose's point. Attend: 0: 0123 3210 big-endian little-endian In both cases, if &input == 0, and (cvptr = (char *) &input) == 0, then cvptr[0-3] will map in the same order to bytes[0-3]; and the copy succeeds. Perhaps he is thinking that (char *) &input == 3, and (short int *) &input == 2? This is exactly the behaviour that big-endians do n o t do, and which little-endian PDP-11 and VAX-11 programmers despair of because thay can't perform type punning. Incidentally, don't call an 11 a PDP, because there are radically different architectures on the PDP-1,-4,-5,-6,-7,-8,-9,-10,-12,-15, -16,-20,... ;-) -- Joe Yao hadron!jsdy@seismo.{CSS.GOV,ARPA,UUCP}