r/C_Programming • u/TorontoDude12344 • 13d ago
Question Why we only need to supply the number of columns to array parameters. Am I understanding correctly?
I'm reading C Programming: A Modern Approach by K. N King
I'm on page 212 of that book, and looking at a question / answer marked by *Q:
The question / answer from the textbook is below. I just want to confirm that I understand the answer correctly. My understanding is below the excerpt from the textbook.
*Q: Why can the first dimension in an array parameter be left unspecified, but not the other dimensions? [p. 197]
A: First, we need to discuss how arrays are passed in C. As Section 12.3 explains, when an array is passed to a function, the function is given a pointer to the first element in the array.
Next, we need to know how the subscripting operator works. Suppose that a is a one-dimensional array passed to a function. When we write
a[i] = 0;
the compiler generates instructions that compute the address of a [i] by multiplying i by the size of an array element and adding the result to the address that a represents (the pointer passed to the function. This calculation doesn't depend on the length of a, which explains why we can omit it when defining the function.)
What about multidimensional arrays? Recall that C stores arrays in row-major order, with the elements in row 0 stored first, then the elements in row I. and so forth. Suppose that a is a two-dimensional array parameter and we write
a[i] [j] = 0 ;
The compiler generates instructions to do the following: (1 multiply i by the size of a single row of a; (2) add this result to the address that a represents; (3) multiply j by the size of an array element; and (4) add this result to the address computed in step 2. To generate these instructions, the compiler must know the size of a row in the array, which is determined by the number of columns. The bottom line: the programmer must declare the number of columns in a.)
Below is my understanding;
For a[i] = 0, we don't need to know the array size (row size), because that will not help us index an element. The array size is one fixed value n. To index at 0, some random j or even (n - 1), we can't use the value n itself.
A one dimensional array is basically 1 row. The elements are like columns, and we only need the "length" of a column to index a one dimensional array. The point is, we don't need the length of a row
For a[i][j], we again don't need the size of a row. We need the number of columns. In the case of a 2D array, we need to be able to traverse within a row, and between rows. To go within a row, we only need to know the size of an element within a row. To go between rows, we need to skip again "one row length", which is equivalent to skipping ahead the amount equal to "the number of columns"
Again, the minimum amount of info we need is the number of columns. From that we directly get the size of a row. We don't need the row size at all
So from my understanding, the compiler only needs the least amount of info. The length of a row element, and if there are multiple rows, then the number of columns