Expert C Programming

(Jeff_L) #1

addr (ca[1]) = 0x20901


++ca = 0x20901


addr of ptr param = 0xeffffa14


addr (pa[0]) = 0x20900


addr (pa[1]) = 0x20901


++pa = 0x20901


It looks a little weird at first sight, to see the address of an array parameter not equal to the
address of the first element of the array parameter, but it's true enough.


You can win a lot of money-making bets with novice C programmers about what the sizeof
operator will produce in this circumstance.


Chapter 10. More About Pointers


Never forget that when you point the finger at someone, three of your own fingers are pointing back at
you...


—Suspicious spy's proverb


the layout of multidimensional arrays...an array of pointers is an "Iliffe vector"...using pointers for ragged arrays...passing a one-dimensional array to a
function...using pointers to pass a multidimensional array to a function...using pointers to return an array from a function...using pointers to create and use
dynamic arrays...some light relief—the limitations of program proofs


The Layout of Multidimensional Arrays


Multidimensional arrays are uncommon in systems programming and, not surprisingly, C does not
define the elaborate runtime routines that other languages require to support this feature. For some
constructs, like dynamic arrays, programmers must allocate and manipulate memory explicitly with
pointers rather than have the compiler do the work. For another construct (multidimensional arrays as
parameters), there is no way of expressing the general case in C at all. This chapter covers these issues.
By now, everyone is familiar with the layout of a multidimensional array in memory. If we have the
declaration


char pea[4][6];


some people think of the two-dimensional array having its individual


rows laid out in a table, as in Figure 10-1.


Figure 10-1. Presumed Memory Layout of a Two-dimensional Array
Free download pdf