C array follies 1

Wayne Throop throopw at rtp47.UUCP
Thu Sep 5 06:29:51 AEST 1985


All right.  Put on your C array thinking caps.

Consider the following program:

    void f(x)
        int x[2][2][2];
    {
        printf( "%o %o %o\n", x, x[0], x[0][0] );
        printf( "%d %d %d\n", sizeof(x), sizeof(x[0]), sizeof(x[0][0]) );
    }
    void main(){
        int x[2][2][2];
        printf( "%o %o %o\n", x, x[0], x[0][0] );
        printf( "%d %d %d\n", sizeof(x), sizeof(x[0]), sizeof(x[0][0]) );
        f(x);
    }

Clearly the printfs are "illegal", but assume you have a fairly vanilla
machine.  Eight-bit bytes, four-byte ints.  Four-byte pointers.
Pointers and ints stored in the same way for argument passing.

Two questions:
  - What does this program print?
    (Especially what is printed on the last output line?)
  - Is this "correct"?  (Again, especially the last line.)

I think that most systems will print "4 16 8" (or equivalent) as the
last line.  While this is (probably) not a bug, I think it is at least a
misfeature.  This same point was part of the "C bites" topic, which is
what drew my attention to it.  The point is *should* C bite in this way,
and if so, why?
-- 
Wayne Throop at Data General, RTP, NC
<the-known-world>!mcnc!rti-sel!rtp47!throopw



More information about the Comp.lang.c mailing list