Expert C Programming

(Jeff_L) #1

The dimensions of the array that the pointer points to make a big difference. Using the declarations in
the example above,


r++;


t++;


will increment r and t to point to their next respective element, (in


both cases, this element is itself an array). The increment will be


scaled by quite different amounts, because r points to array elements


that are three times larger than the array elements to which t points.


Programming Challenge


Hooray for Arrays!


Using the declarations:


int apricot[2][3][5];


int (*r)[5] = apricot[0];


int *t = apricot[0][0];


write a program to print out the initial values of r and t in hex (use the %xprintf conversion
character to print a hex value), increment these two pointers, and print out their new values.


Before running the program, predict how many bytes the increment will actually cause to be
added to each pointer. Use Figure 9-8 on page 255 to help you.


How Arrays Are Laid Out in Memory


With multidimensional arrays in C the rightmost subscript varies fastest, a convention known as "row
major addressing". Since the "row/column major addressing" term really only makes sense for arrays
with exactly two dimensions, a preferred term is "rightmost subscript varies fastest". Most algorithmic
languages use "rightmost subscript varies fastest", the major exception being Fortran, which prefers
leftmost, or column major addressing. The subscript that varies fastest makes a difference to the way
in which arrays are laid out in memory. Indeed, if you pass a C matrix to a Fortran routine, the matrix
will appear transposed—a huge kludge, advantage of which is occasionally taken.


Figure 9-9. Row Major versus Column Major Order
Free download pdf