3

I'm trying to understand how to set a column of a two dimensional array to 0.

I follow this code snippet from C programming textbook by K.N. King.

   int a[NUM_ROWS][NUM_COLS], (*p)[NUM_COLS], i;
   ...
   for (p = &a[0]; p < &a[NUM_ROWS]; p++)
      (*p)[i] = 0;


I genuinely don't understand how this works. Greatly appreciate any clarification.

1 Answer 1

3

It's all related to how an array is converted to a pointer on access, see: C11 Standard - 6.3.2.1 Other Operands - Lvalues, arrays, and function designators(p3).

In your case you have a two dimensional array of int a[NUM_ROWS][NUM_COLS];. Which in reality is an array of arrays of int[NUM_COLS]. (an array of 1D arrays).

When you access a, a is converted to a pointer to the first 1D array and is of type int (*)[NUM_COLS] (a pointer to an array of NUM_COLS integers).

You declare p as a pointer to an array of NUM_COLS integers, so p is type compatible with a. You can simply initialize:

p = a;

(instead of p = &a[0];)

In your for loop you loop from p = a; (a pointer to the first 1D array), and loop while p is less than &a[NUM_ROWS] (the address 1-after the final 1D array) incrementing p each iteration (and since p is a pointer to int[NUM_COLS], p points to the next row each time you increment p)

When you dereference p you have an array of int[NUM_COLS], so when you address (*p)[i] = 0; you are setting the ith element of that row to 0.

That's it in a nutshell. Let me know if you are still confused and where and I'm happy to try and explain further.

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

5 Comments

What does &a[NUM_ROWS] mean? Does it same as a[NUM_ROWS](pointer to int 1d array)?
&a[NUM_ROWS] is the address of a[NUM_ROWS]. Which the only valid indexes for the rows in a are 0 -> NUM_ROWS-1, so a[NUM_ROWS] is one past the end of a which is a valid address for the array, but not one that holds a value. In effect it can be used to mark the end of the array only. The [..] operates as a dereference. so a[NUM_ROWS] would be a 1D array which on access is converted to a pointer to the first element in the NUM_ROWS row (which as discussed is the first address after a).
@DavidC.Rankin Your clarification makes perfect sense to me. Thank you so much!
Glad to help. This is one area that catches all new C/C++ programmers. So might as well go ahead and learn it -- it will save you untold hours of head-scratching later... :) YES, &a[NUM_ROWS] is simply a + NUM_ROWS. (recall a[NUM_ROWS] == *(a + NUM_ROWS) so &*(a + NUM_ROWS) == a + NUM_ROWS.)
Yes, you got it. a on access is a pointer to an array of NUM_COLS integers. You have NUM_ROWS of those. With pointer arithmetic set by type (type is king), p being a pointer to an array of NUM_COLS integers means p++ advances p to point to the next array of NUM_COLS integers (the next row in a).

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.