C Tutorial, Part III



The C "String" Type (Array of char, or char *)


Arrays of strings (2-D array of char)


File I/O and 2-D arrays


Answers to Pop Quizzes:

  1. matrix[1,2] means the same thing as matrix[2]! Look at the comma operator (K&R, p.62).
  2. Because we want to modify its value.

  3. Because without this dimension the compiler could not resolve expressions in the receiving function like matrix[i] because it would not know where the i'th row starts. The compiler computes the address of the i'th row as follows:

    Assume the name of the 2-D array is matrix. The compiler gets the address of the first element of the first row from the value of the name matrix because its name is the address of the first element. Then the compiler multiplies the column dimension specified in the prototype by the index given in the square brackets to calculate the offset from the beginning of the array. The sum of the two is the start address of the i'th row in the matrix. Thus, the expression matrix[i] produces the address of the i'th row.

  4. All the string functions care about is a null character ('\0') which marks the end of the string.