0

I'm trying to remove a char from an array and I'm having problems. I read how to do it with numbers but it seems is different from removing chars as it's not working for me.

If I've the following array:

char stuff[][20] = {"one", "two", "three", "four", "five"};     

How can I remove the element "four" for example, so I can get

{"one", "two", "three", "five"};   
2
  • 2
    Let the compiler count for you char stuff [][20] = ... Commented May 21, 2013 at 14:05
  • 2
    How do you do it with integers? It's not that much different with strings (just remember a string is an array) Commented May 21, 2013 at 14:11

3 Answers 3

3

You could do this by dynamically allocating all the needed memory: an array of char* as well as memory for the strings.

Then you could create another array without the item to be removed and copy all data that remains. Finally free the original array and strings.

It's a bit complex but not too hard. It may be easier if you could create a linked list - but this will probably change your application logic a bit more.

Sign up to request clarification or add additional context in comments.

4 Comments

Is that hard? I mean, Isn't there a "remove" function that using the index you can remove that element?
@Borja, nope. C programming is a lot of 'do it yourself'.
I see. It would be easier if I use vectors rather than array's to do this?
Then you may use std::list - with vector you cannot remove inbetween.
2

You're going to have to copy the strings, and keep track of how many you consider to be present since the allocated space itself is not going to go away.

Comments

1

You can't change the size of an array in C; as declared, your stuff array will always have 5 elements (where each element is a 20-element array of char).

What you can do is copy the contents of the fifth element to the fourth element:

strcpy(stuff[3], stuff[4]); // overwrites "four" with "five"

and then empty out the fifth element:

strcpy(stuff[4], ""); // zeros out the first element in the subarray

or

memset(stuff[4], 0, sizeof stuff[4]); // zeros out entire subarray

Either way, your array will now contain the strings

{ "one", "two", "three", "five", "" }

If you really want to change the number of elements in the list (not just clear out the last one), you'll have to use a different strategy.

Problems like this usually call out for a data structure known as a linked list; each element in the list explicitly points to the next element. You can add, remove, and re-order elements in a linked list much more easily than you can in an array (even though an array can be used as the backing store). There are a number of examples (of varying quality) on the Web. However, if you can find a copy of Sedgewick's "Algorithms in C", that would be a better resource.

1 Comment

Yes, I need to change the number of elements, so the index changes as well. Thanks for the recommendation. Will try linked list.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.