tags:

views:

582

answers:

4

Guys,

I have a loop that populates "char array_of_strings[100][100];" At some point I want to be able to clean it from all the strings added so far and start adding from position 0. How can I clean/rest it in C?

Thanks

+3  A: 

That data structure really has no inherent structure. You basically reset it by pretending there's nothing in it.

You have some counter that tells you which of the strings you've populated, right? Set it to zero. Done.

chaos
What do you mean exactly by "reset it by pretending there's nothing in it."? I got get it.
goe
Your `array_of_strings` is nothing but 10000 contiguous bytes. Its data only has any organization by virtue of however your code understands it to have organization, e.g. by you having a variable `strings_populated` or something that tells you you've put strings in some N of the potential locations. Since that's all that's giving it structure, you can wipe out the structure by wiping that out.
chaos
well if I set strings_populated back to 0 then I will start overwriting what's already in the array, true. but what if I don't overwrite all of the previously added strings. Then I'm left with lets say 10 new strings and 2 from the old loop.
goe
Your code will only access the 'old' strings if you, for some reason, allow it to. Normally I would assume that after the operation you describe, the counter would tell you that you have 10 strings populated and there's no reason you'd go beyond that.
chaos
Another way of saying what I'm trying to get at here is that whatever data exist in the allocated space, outside of the controlling elements that your code uses to decide what to work on, *doesn't matter*. For an individual string, the controlling element is its zero terminator. For the set of strings, the controlling element is your population counter. The allocated memory outside of that could be filled with rainbows and ponies for all you care, because you'll never look at it.
chaos
(This is somewhat particular to cases where you just have a big chunk of memory that you're basically using as a scratch space -- for example, allocating a `char[][]` on the stack. If you were allocating and deallocating individual strings, it'd be different.)
chaos
+7  A: 

You can use the memset function to set all characters back to zero.

P.S. Using a two-dimensional array is fairly unconventional for dealing with strings. Try moving towards dynamic allocation of individual strings.

Martin v. Löwis
+2  A: 

Assuming that you are in fact using the array as strings, then something like this should work:

int i;
for (i=0; i<100; i++)
   array_of_strings[i][0] = 0;

Of course, it you aren't treating your data as strings, then you might need to look at something like memset.

Tom
even if you *are* treating the data as strings, memset seems more elegant.
Nathan Fellman
I agree with @Nathan Fellman above. It's less ambiguous as to what you're trying to accomplish when you memset(..) an array to zero.
Andrew Song
+1  A: 
memset(array_of_strings, 0, sizeof(array_of_strings));

This is cleaner than putting a magic number for the size in there, and less likely to break down the road when someone changes the size of your strings array. Since you know the size of the array at compile time using sizeof will work.

You can also use bzero, which is a lot like memset, but only for zeroing. On some platforms bzero may be faster than memset, but honestly both functions are so fast, splitting hairs here is silly.

bzero(array_of_strings, sizeof(array_of_strings));

bzero requires you to #include memset needs #include

The memset man page is here

The bzero man page is here

Aftermathew