int color[1001][1001];
int m,n;
m=10;
n=10;
memset(color,0,sizeof(color));
memset(color,0,sizeof(color[0][0])*m*n );
What's the difference between these two memset statements?
Any answer will be highly appreciated. Thanks in advance.
What's the difference between these two memset statements?
The memset
function takes, the destination, value and count. The count is sizeof(color)
which would be sizeof(int) * 1001 * 1001
for the first call.
For the second it will be sizeof(int) * 10 * 10
.
The former clears the complete array with zeros, while the latter does it only partially, starting from color[0][0]
to color[0][99]
, which relies on the fact that arrays are laid out in a row-major fashion. Relevant excerpt from the C11 standard (draft n1570), §6.5.2.1 Array subscripting:
[…] It follows from this that arrays are stored in row-major order (last subscript varies fastest).
Alternatively, if m = n = 1001
i.e. m
and n
actually denote the array's dimensions, the two calls then are the same, just two different ways of writing it.