0

I'm reading a book about C programming, and I'm not sure whether there is an error in the book or not. Its about arrays and has the following array example:

enter image description here Then it says:

The following statements sets all the elements in row 2 of array to zero: for( column = 0; column <= 3; column++) a[ 2 ][ column ] = 0;

The preceding for statement is equivalent to the assignment statements: a[ 2 ][ 0 ] = 0; a[ 2 ][ 1 ] = 0; a[ 2 ][ 2 ] = 0; a[ 2 ][ 3 ] = 0;

Shouldn't it say "The following statements sets all the elements in row 1 to zero"?. Because if I say a[ 3 ] I am talking about the row 2, if I say a[ 2 ] I am talking about row 1 and if I say a[ 1 ] I am talking about row 0.

4
  • 1
    "Shouldn't it say "The following statements sets all the elements in row 1" instead of 2" - Do you mean, "in row 3"? Nevertheless, I don't think this is a mistake. They may use zero-based counting too. Commented Jun 6, 2014 at 3:03
  • The book's text is speaking in terms of 0 indexed to match the code. Commented Jun 6, 2014 at 3:08
  • The book is consistent with how C indexes and its own figure. Commented Jun 6, 2014 at 3:23
  • "if I say a[ 1 ] I am talking about row 0." - no, a[0] talks about row 0. Just as in the diagram in your question. Commented Jun 6, 2014 at 4:39

5 Answers 5

1

C uses row-major order and zero-based indexing to reference array indices. The description you've provided of what is in the book is consistent with this.

Sign up to request clarification or add additional context in comments.

Comments

1

I would assume that the book means there are actually 3 rows in the array: #0, #1, #2. They are modifying row #2 which is the third row.

Comments

1

Shouldn't it say "The following statements sets all the elements in row 1 to zero".

No. It shouldn't. In C, array indexing starts from 0. In the same book at page number 196 author said that:

The first element in every array is the zeroth element. Thus, the first element of array c is referred to as c[0], ...

Comments

0

The author seems to be counting from zero, just like how arrays start from index 0 instead of index 1 in c.

As one of the comments asks: Do you mean to wonder if the author should say "row 3" instead of "row 2" since the row is indexed by 2 and so is the third row in the array?

Anyways, There is not an error in the book.

Comments

0

Which row is "row 2" is a matter of convention. The picture you posted establishes that convention pretty clearly and unambiguously: rows are numbered from 0. This means that a[0][i] refers to elements in row 0, while a[2][i] refers to elements in row 2. For that reason I don't see anything wrong in that quote.

Moreover, I don't see how it could possibly be "row 1" under any meaningful convention. I wouldn't be surprised to see someone call it "row 3", but not "row 1"... Why "row 1"? Your assertion "if I say a[ 3 ] I am talking about the row 2, if I say a[ 2 ] I am talking about row 1 and if I say a[ 1 ] I am talking about row 0" suffers from the same problem. I don't see any logic in it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.