Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!seismo!sundc!pitstop!sun!decwrl!labrea!aurora!ames!lll-tis!ptsfa!ihnp4!homxb!mtuxo!mtune!codas!usfvax2!jc3b21!crash From: crash@jc3b21.UUCP (Frank (Crash) Edwards) Newsgroups: comp.unix.wizards Subject: Re: have I found a bug in K&R? Message-ID: <190@jc3b21.UUCP> Date: Sun, 11-Oct-87 13:18:25 EDT Article-I.D.: jc3b21.190 Posted: Sun Oct 11 13:18:25 1987 Date-Received: Wed, 14-Oct-87 05:37:57 EDT References: <517@hubcap.UUCP> <321@laticorp.UUCP> Organization: Transportation Systems Consulting, Palm Harbor, FL Lines: 53 Keywords: argv definition Summary: not necessarily... In article <321@laticorp.UUCP>, sarah@laticorp.UUCP (Sarah Groves Hobart) writes: > What is actually being declared is a pointer to an array of pointers. > Remember that the following declarations are equivalent: > > char s[]; > > char *s; > > Most people would tend to call the first declaration a character string, > and the second a character pointer, but they are the same thing. So K&R > are just using slightly different terminology, probably in the interests > of clarity. > > Sarah Groves Hobart > {ihnp4,amdahl,sun}!ptsfa!laticorp!sarah They are the same but they are not the same! Using the strategy above, try the following: ---------- foo.c: char s[] = "This is the character array."; bar.c: extern char *s; ---------- There is a subtle but startling difference between the two; primarily in the storage allocated for each. The declaration in "foo.c" defines storage for the string and sets the array variable s to that address. The extern decl- aration in file "bar.c" indicates that s is an n-byte pointer to the string. This means that to print the string, say, the compiler finds the address of s and the code generated loads a pointer value from it, whereas for a true array the compiler simply uses the value of s at compile time. Perhaps this is obvious to most ;-) but it wasn't to me. My particular run-in with this type of coding was more devious because I declared an array of character strings (similar to argv...) and then referenced them as "ptr to ptr to char". I should note that there won't be a problem if these constructs are used within function calls since the array reference generates an address (for case #1), and the compiler code loads the ptr from address s (for case #2). And so either way a parameter declared as "char *s" would work. This problem only crops up when global declarations are used. Anyway, I hope the above is intelligible. If not, oh well. Frank (Crash) Edwards