Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!seismo!mimsy!chris From: chris@mimsy.UUCP (Chris Torek) Newsgroups: comp.lang.c Subject: Re: *\"LDA\" ok? Message-ID: <8088@mimsy.UUCP> Date: Sat, 22-Aug-87 02:33:22 EDT Article-I.D.: mimsy.8088 Posted: Sat Aug 22 02:33:22 1987 Date-Received: Sun, 23-Aug-87 10:18:38 EDT References: <8877@brl-adm.ARPA> Organization: U of Maryland, Dept. of Computer Science, Coll. Pk., MD 20742 Lines: 73 In article <8877@brl-adm.ARPA> ADLER1%BRANDEIS.BITNET@wiscvm.wisc.EDU writes: >I was trying to write a C program that would read MIX commands from >stdin. I also wanted to be able to verify that the string opcode >was actually internally equal to the string LDA.... > char opcode[4]; > int address, index, left, right; > > printf("Type assembly language statement:\n\n"); > scanf("%s %d,%d(%d:%d)",opcode, &address, &index, &left, &right); > if (*opcode == *"LDA") printf("Gotcha!\n"); > else printf("No match...\n"); No doubt this has already been answered in mail directed to adler1@brandeis.bitnet, but I want to expand on this a bit. Aside from the missing test for scanf's return value, this code can be called correct: there is nothing a typechecker like lint could diagnose, for instance. Yet it does not do what was desired. To compare the characters in `opcode' with the string "LDA" for equality, one should use if (strcmp(opcode, "LDA") == 0) which is such a common idiom that old-time C programmers understand it at a glance. It seems to come late to neophyte programmers, though, and it seems reasonable to ask why. Perhaps it is because other languages provide string comparison within the language itself: if opcode stringequal "LDA" then ... or if opcode = "LDA" then ... A straightforward (but wrong) translation yeilds if (opcode == "LDA") ... which is syntactically and semantically valid, but is always false (or usually false in some compilers, and certainly false in this case.) Programming by patching (a technique familiar to mathematicians as well, in the form known as `proof by patching': `oops, well for case 2, change the original equation to . . .') leads to if (*opcode == *"LDA") which works for some test cases, since it compares opcode[0] with 'L'. I have even seen something like if (*opcode == *"LDA" && *(opcode + 1) == *("LDA" + 1) && *(opcode + 2) == *("LDA" + 2)) which works for even more test cases, but is still wrong as well as wasteful (at least in compilers for which "LDA"=="LDA" is false). Eventually it seems to dawn upon these programmers that "LDA" generates an anonymous character array holding the letters L, D, A, and NUL (\0) and evaluates to the address of this array. Then the purpose of strcmp() becomes clear, and they live happily ever after :-). All I want to know is this: Why does it take so long for some programmers to see this, and how can we speed up the process? -- In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7690) Domain: chris@mimsy.umd.edu Path: seismo!mimsy!chris