C pointer problems in VMS.

Karl Heuer karl at haddock.ISC.COM
Mon Mar 21 05:43:21 AEST 1988


In article <25667 at cca.CCA.COM> g-rh at CCA.CCA.COM.UUCP (Richard Harter) writes:
>In article <12464 at brl-adm.ARPA> V053MF43 at 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 at haddock.isc.com), The Walking Lint



More information about the Comp.lang.c mailing list