ProgrammingHard
Which statement about memset(a, 0, sizeof a) for a 2D int array a[4][5] is correct?
- A. It sets every element of the whole array to zero, since sizeof a covers all rows
- B. It zeroes only the first row, because a is a pointer
- C. It is undefined for 2D arrays
- D. It zeroes only the first column
Correct answer: A. It sets every element of the whole array to zero, since sizeof a covers all rows
a is a real array object, so sizeof a is the total size in bytes, and memset clears the entire block.
More C Programming questions
- int a[3][4]; int p = &a[0][0]; Which expression refers to a[2][1]?
- On a machine with 4-byte int, what does sizeof(r) give when int (r)[3] is a pointer to a row of three ints?
- Three rows are each malloc'd separately, and their pointers are stored in an array rowp[3]. Which cleanup order is correct?
- A matrix of n x n values is read and the program needs the sum of all cells above the main diagonal. What is the cost, and is it correct…
- For a 2D prefix sum with P[i][j] = sum of the first i rows and j columns, what is the cost of building it for an n x n grid?
- int grid[4][4] is filled by grid[i][j] = i j. What is grid[3][2]?