Reputation: 1243
I have a question here . Now I wish to clear every element of the array without the use of setting the '\0' trick. I tried with memset, but it complains of
expected ‘void *’ but argument is of type ‘struct array’
here is my code:
int i;
for( i=0; i< sizeof(array)/sizeof(array[0]) ; i++){
memset(array[i], 0, sizeof(array[i] ) );
}
how to properly clear it. thanks
Upvotes: 2
Views: 4123
Reputation: 7960
Since you have a static array there's no need for a for
loop:
memset(array, 0, sizeof(array)); // sizeof gives proper size of entire array if array is static.
Upvotes: 0
Reputation: 16043
memset
expects a pointer (add &
):
memset(&array[i], 0, sizeof(array[i] ) );
But if you are going to clear the entire array, you could replace the for
-loop with:
memset(array, 0, sizeof(array));
As a side note: You should really avoid sizeof(array)/sizeof(array[0])
. Define constant for element count. It is less error prone when you start passing arrays to functions.
Upvotes: 4