Fastest way to zero out a 2d array in C?

Posted by Eddy on Stack Overflow See other posts from Stack Overflow or by Eddy
Published on 2010-03-25T13:57:20Z Indexed on 2010/03/25 14:03 UTC
Read the original article Hit count: 274

I want to repeatedly zero a large 2d array in C. This is what I do at the moment:

for(j = 0; j < n; j++)
{
    for(i = 0; i < n; i++)
    {  
        array[i][j] = 0;
    }
}

I've tried using memset:

memset(array, 0, sizeof(array))

But this only works for 1D arrays. When I printf the contents of the 2D array, the first row is zeroes, but then I got a load of random large numbers and it crashes.

© Stack Overflow or respective owner

Related posts about c

    Related posts about multidimensional-array