Path: utzoo!attcan!uunet!seismo!sundc!pitstop!sun!decwrl!purdue!bu-cs!dartvax!eleazar.dartmouth.edu!earleh From: earleh@eleazar.dartmouth.edu (Earle R. Horton) Newsgroups: comp.lang.c Subject: Re: What does Z["ack"] = 5 mean? Keywords: Obfuscated C, wierd Message-ID: <10283@dartvax.Dartmouth.EDU> Date: 5 Oct 88 13:57:28 GMT References: <14999@agate.BERKELEY.EDU> Sender: news@dartvax.Dartmouth.EDU Reply-To: earleh@eleazar.dartmouth.edu (Earle R. Horton) Organization: Thayer School of Engineering. Lines: 46 In article <14999@agate.BERKELEY.EDU> laba-3aw@web.berkeley.edu (Sam Shen) writes: >Exactly what does this mean: > >main() >{ > char Z; > > Z["ack!"] = 5; >} > Portion of core dump obtained by running this program on a VAX, and core dumping before main() returns, follows: \300^C\214\350\377^?^E^@^@^@^Eck!^@^@^@ ^-- There's your 5! What happened here was that Z was initialized to (char)0 by the compiler or loader on the VAX. (BSD 4.3) Then, Z["ack!"] was taken to mean 0["ack!"] which of course means 0[
]. The equivalence sets the first character in "ack!" equal to 5, which is what you see in the core dump as displayed by emacs. This is of course grossly implementation dependent, and uses questionable programming practices, to wit: Casting of char to pointer is highly questionable. Said pointer defaulting to (char *) is probably reliable, but not good programming practice. Assuming you can write on top of constant data may not work on all systems. The assumption that Z will be equal to '\0' if not explicitly initialized is not portable. Worse, you cannot cast a pointer ("ack!") to an integer array subscript on some systems! Good programming practice demands you say exactly what you mean, and only write on variable data (usually). Thus: main() { char Z[5] = "ack!"; Z[0] = 5; } Earle R. Horton. 23 Fletcher Circle, Hanover, NH 03755 (603) 643-4109 Sorry, no fancy stuff, since this program limits my .signature to three