"Michael Comperchio" <mcmp...@...> wrote: > /********* > > A reference to an object of type array-of-T which appears > in an expression decays (with three exceptions) into a > pointer to its first element; the type of the resultant > pointer is pointer-to-T. > > That is, whenever an array appears in an expression, the > compiler implicitly generates a pointer to the array's > first element, just as if the programmer had written &a[0]. > > *********/ > > This is a truth.
Indeed. > In multidimensional arrays, all but the lowest level > array contain pointers to the beginning of the next > level.. No, you're misinterpreting. That arrays decay to pointers (unless they are operands of & or sizeof) does not mean pointers are stored within the array. It simply means that arrays are labels and that access is achieved via arithmetic on a pointer to the first element. --- There seems to be a simply misunderstanding of what a 'pointer' is here. A 'pointer' is a piece of memory containing the address of another piece of memory ( it 'points' to it). The first (second, third..) level of a multidimensional array contains the memory addresses of the next level. > I wish I was better at ascii graphics, I could draw > this out.. The declaration... int a[2][3]; ...is... |<--------- [0] ---------->|<---------- [1] --------->| +--------+--------+--------+--------+--------+--------+ | [0][0] | [0][1] | [0][2] | [1][0] | [1][1] | [1][2] | +--------+--------+--------+--------+--------+--------+ --- You may find it useful to think this as you work with it, but in reality there is NO guarantee that this will be so. In today's massive memory spaces this is more likely to be so, but NOT guaranteed. ...not... +-----+-----+ | [0] | [1] | +-----+-----+ | | | +--------------------+ v v +--------+--------+--------+--------+--------+--------+ | [0][0] | [0][1] | [0][2] | [1][0] | [1][1] | [1][2] | +--------+--------+--------+--------+--------+--------+ ...and certainly not... +-----+-----+ | [0] | [1] | +-----+-----+ | | | +------+ | v | +--------+--------+--------+ | | [1][0] | [1][1] | [1][2] | | +--------+--------+--------+ v +--------+--------+--------+ | [0][0] | [0][1] | [0][2] | +--------+--------+--------+ -- This is the layout that is, in fact, what multi dimension array access is best thought of. > The compiler will generate access code that looks > something like this for multidimensional array access: > > *(*(level1 + n) + n)... Yes, 'level1' is converted to a pointer to the first element. It may be useful to come full circle and view this as... * ( * (&level1[0] + n ) + n ) -- And is &level1[0] not the same thing as level1? I know I've been doing this for a long time, but I don't think I've missed any fundamental changes it the language.. In a sense, level1 in the first expression is no longer an array. It is not the array which is being dereferenced, but a pointer to the first element. So yes, there is a pointer _value_ involved, but that value is not part of the array itself, rather it's a reference to the array. > Try preprocessing to assembler ITYM Try disassembling. --- That's fine and dandy, but that's not the output from the compiler, and NOT necessarily an accurate representation of the original coding. > and check out the resulting code. My advice to you is don't. -- My advice is that people learn ASM so you can easily do this. It will give you a much better understanding of how the machine works, AND how compilers work. Don't they teach compiler theory out of the Dragon book anymore? > It is this that I was referring to. It also behooves me > to remember that arrays in c are contiguous blocks of a > type. That's all. They really don't have any more of a > special meaning than that. Your other statements directly contradict that > When we are coding in c we are coding at a lower level > that C#, or VB. No, we're not!! That is a mistake many authors delude people into thinking. Why? Because they simply don't know any better! C is a 4th Generation Programming Language. It is no less abstract than lisp, perl, COBOL, prolog, ml, etc... C is a systems programming language (http://en.wikipedia.org/wiki/History_of_programming_languages). C IS a lower level language, targeted at a different place in the grander scheme of things. Because of all the 'add on' libraries available it can be used to create really cool application programs, but that was not its original intention. I have coded in lisp, perl, COBOL, RPG, GWBASIC, VB, C#, C++, FORTAN, ASM (x86, z80, m68k, 360/370) .. Try writing an operating system in any of those languages (well ok, in ASM yes). C was written to write UNIX. The complete operating system (with the exception of a few hundred lines of ASM to bootstrap the machine) is written in C. I would challenge you write an operating system in COBOL...or a Compiler for that matter.. ugh, what a painful thought. Michael -- Peter [Non-text portions of this message have been removed]
