Path: utzoo!mnetor!uunet!husc6!mit-eddie!uw-beaver!cornell!rochester!udel!burdvax!sdcrdcf!ism780c!haddock!karl From: karl@haddock.ISC.COM (Karl Heuer) Newsgroups: comp.lang.c Subject: Re: C pointer problems in VMS. Message-ID: <3072@haddock.ISC.COM> Date: 20 Mar 88 19:43:21 GMT References: <12464@brl-adm.ARPA> <25667@cca.CCA.COM> Reply-To: karl@haddock.ima.isc.com (Karl Heuer) Organization: Interactive Systems, Boston Lines: 40 In article <25667@cca.CCA.COM> g-rh@CCA.CCA.COM.UUCP (Richard Harter) writes: >In article <12464@brl-adm.ARPA> V053MF43@ubvmsc.cc.buffalo.EDU (Mike Ayers) writes: >o ... with sundry problems >>char *wr(a) int a; { body } >>main() { > <------- char *wr() goes here, your code declares wr as > a function returning an int (the default if there > is no declaration.) True, if wr() and main() were in separate files. I assume from the way the question was asked that the definition of wr() appeared above main() in the same file, in which case the definition itself serves as a declaration. (Also, I consider it bad practice to declare a function with local scope, so I'd put it outside main() anyway, even if nobody else calls it.) Even if that mistake was made, my knowledge of the VAX architecture suggests that it would not have caused the problem, so I suspect that the bug lies in the body of wr(). >>main() { >> char b[12]; ... >> strcpy(((&b)+1),"rf!"); >The expression &b should be &b[0]. b is an array and is a pointer (sort of >:-)). You are passing a pointer to a pointer. You're right about the bug and the fix. But &b is not a pointer to a pointer, it's a pointer to an array (assuming the compiler handles this properly; PCC doesn't). The strcpy() function needs a pointer to a char, which is what &b[0] yields. Lint would have caught this -- doesn't DEC make a linter yet? >I say 'sort of' because there are subtle differences between arrays and >pointers in C [which I won't try to explain here]. I won't go through the whole explanation, but here's the Reader's Digest version for those who are interested. Arrays and pointers are completely different entities. In an rvalue context, an array expression is converted into a pointer to its first element. This is somewhat analogous to the way a char expression is converted to int. Karl W. Z. Heuer (ima!haddock!karl or karl@haddock.isc.com), The Walking Lint