0

Whats the best way to removes values from array of strings? Is it better to create a new array of strings or is there some way to remove them?

4
Contributors
7
Replies
35
Views
6 Months
Discussion Span
Last Post by AssertNull
0

Pretty easy. Set the array to a nul string and deal with it in your code.

BTW, this is usually done in a linked list or say, a string library function but for me, I've used the old nul means no string here method for a very very long time.

So I'm going with:

  1. Use a linked list. (Sorry but you can google this.)
  2. Use my old nul method and deal with it when printing, searching, etc.
  3. Use your method but speed can take a huge hit as you may have thousands of strings to move.
0

If you don't care about the order of the strings, just keep track of the number of strings and replace the address of the deleted string with the address of the last string. Then decrease the count by one. If order is important than use a linked list as suggested by rproffitt. It's a little more work but making a library of linked list functions is worth the trouble for what you will learn by doing it.

Edited by Reverend Jim

0

I am the master of segmentation faults when doing linked lists so I don't really like this option.

Setting to null sounds easy :).

I only have an array of size 300 so not really worried about this.

1

Which method do you usually use?

buffer[0][0] = '\0';
memset(buffer,0,strlen(buffer[0]));
memset(buffer,0,sizeof(buffer[0]));
Votes + Comments
Item 1. But Code Police may issue a ticket.
0

What method would code police like? I can't think of any other way to do this but one of these methods.

The wall warning of gcc doesn't like the third method some reason.

 warning: argument to ‘sizeof’ in ‘memset’ call is the same expression as the destination; did you mean to provide an explicit length? [-Wsizeof-pointer-memaccess]
0

Why isn't the memset a simple length of 1? C style strings end with zero so why fill the entire space?

Seems like this sort of programming is the reason code today is often slower than what we wrote decades ago. That is, coders today do the damndest things.

1

Seems like the answer to this question is "It depends", as it is with lots of questions. Linked list versus array? It depends. Move thousands of bytes of memory versus a new array and a big deep copy versus some clever pointer manipulation versus... It depends. On a lot of stuff. More info needed as far as what the scarce resources are, how often deletions happen, etc., etc.

I have to agree with rproffitt on the last point though. I imagine I could come up with a scenario where setting more than one byte to 0 to signify an empty string made sense, but unless there is a very good reason to do so, I don't see the need. The entire point of C strings is that once you hit that NULL terminator, you don't care what may be beyond it.

Votes + Comments
Mr. Monk version: "Zero, zero, zero, zero...."
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.