[comp.lang.c] arrays vs. pointers

drw@cullvax.UUCP (03/12/87)

bill@voodoo.UUCP (Bill Sears) writes:
> in decl.c:
> 	char Big_buf[512];
> 
> in main.c
> 	main()
> 	    {
> 	    extern char *Big_buf;
> 	    int i;
> 
> 	    for (i = 0; i < 512; i++)
> 		*(Big_buf + i) = 0;
> 	    }

The linker should, ideally, reject this.  If you want to say "extern
char *Big_buf" then you have to say "char *Big_buf" where you define
it.  What you should say is "extern char Big_buf[]".  Your second
example (where both defintion and reference were in the second file)
is probably invalid under any reasonably tight definition of the
language, but you compiler happens to let you get away with it.

Dale
-- 
Dale Worley		Cullinet Software
UUCP: ...!seismo!harvard!mit-eddie!cullvax!drw
ARPA: cullvax!drw@eddie.mit.edu
Un*x (a generic name for a class of OS's) != Unix (AT&T's brand of such)

throopw@dg_rtp.UUCP (03/13/87)

> bill@voodoo.UUCP (Bill Sears)
> One problem that I have had concerning arrays and pointers had to do with
> declaring a char array in one module and accessing it as a char pointer
> in another module.  For example:

Not surprising you have a "problem" with it, since it is illegal.  Lint
has this to say about the example you posted.

    main.c
    ==============
    (8)  warning: main() returns random value to invocation environment
    ==============
    value type declared inconsistently
        Big_buf     defs.c(1) :: main.c(7)

>                  This only is a problem if you have the declaration
> and the dereference in separate files.

Well, let's see what lint has to say when everything is in one file:

    main.c
    ==============
    (5)  redeclaration of Big_buf
    (10)  warning: main() returns random value to invocation environment

It's still a problem.  Lint still complains.
You mean you didn't lint it?  Why not?

> Anyone seen any compilers that handle the first example "properly".

Since "proper handling" in this case is the production of an error
message, and since no compiler I'm familiar with reads multiple source
files, I'd have to say that no compiler I'm familiar with handles it
properly.  On the other hand, lint does handle it properly.


What's that?  You say you thought this example was legal?  Why?  You
thought pointers and arrays were the same thing in C?  Foolish earth
creature!  I recommend reading "The C Programming Language", by
Kernighan and Ritchie.  As near as I can tell, this book is a "classic"
by Mark Twain's definition.

By the way, why didn't you just do the obvious thing:

decl.c:
        char Big_buf[512];
main.c:
        main()
            {
            extern char Big_buf[];
            int i;
            for (i = 0; i < 512; i++)
                *(Big_buf + i) = 0;
            return( 0 );
            }

Lint doesn't complain about this at all.

--
Classic: a book which people praise but don't read.
                                --- Mark Twain
-- 
Wayne Throop      <the-known-world>!mcnc!rti-sel!dg_rtp!throopw