3

first question is: is it even possible to do with an array?? I've seen some suggestions saying that it is not to use a list instead of an array, but my problem is i have to use arrays in my program

`for(j=0;j<size_array;j++){
 if(strcmp(a[j],input)==0){
    strcpy(a[j], "\0");
    a[j] = a[j-1];
    size_array--;
}
}`

this is what i have tried and it gives me the following result:

a[0] = "apple"

a[1] = "banana"

a[2] =         //removed item

a[3] = "orange"

is there a way to move "orange" to position a[2] like so:

a[0] = "apple"

a[1] = "banana"

a[2] = "orange"

Thanks in advance

1
  • I'm not sure this isn't an XY problem. Do you want to pop that spot out, or do you want to fix the for loop you posted so that it will move everything from the empty spot? Commented Dec 14, 2013 at 22:16

3 Answers 3

1

I doubt the code you have shown is print the output you are showing, instead it should print

a[0] = "apple"
a[1] = "banana"
a[2] = "banana"
a[3] = "orange"

Anyways ... All you actually need is to find the index of the string you want to remove and then start moving the string after it one step back. after you are done you can decrease the size of your array.

Since you are able to do a[j] = a[j-1] I assume you are using an array of pointers. In this case there is no need for strcpy(a[j], "\0");, just moving the next element one step back is enough. If the removed string is dynamically allocated then you may want to free its memory.

Here is an example which prints the output you are expecting:

#include<stdio.h> 
#include<stdlib.h> 
#include<string.h> 

int main(int argc, char** argv)  
{ 
    int size = 0, capacity = 10, i;
    char** array = malloc(sizeof(char*) * capacity);
    array[0] = strdup("apple");
    array[1] = strdup("banana");
    array[2] = strdup("pear");
    array[3] = strdup("orange");
    size = 4;

    for(i = 0; i < size; i++) {
        printf("%s\n", array[i]);
    }

    for(i = 0; i < size; i++) {
        if(strcmp(array[i], "pear") == 0) {
            free(array[i]);
            break;
        }
    }
    for(; i < size; i++) {
        array[i] = array[i+1];
    }
    size--;


    printf("\n");

    for(i = 0; i < size; i++) {
        printf("%s\n", array[i]);
    }

    return 0;  
} 

This prints:

apple
banana
pear
orange

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

Comments

1

You're looking for the memmove function:

#include <string.h>
.
.
.
remove_item(a, offset);

// memmove(destination, source, nbytes)
memmove(a + offset, a + (offset + 1), (a_len - (offset + 1)) * sizeof a[0]);

For example, if you have 4 items, and you remove item 1 (item numbers are 0..3), you will do:

// memmove(a + 1, a + (1+1), (4 - (1+1)) * sizeof a[0]);
memmove(a + 1, a + 2, 2 * sizeof a[0]);

The multiplication by the sizeof bit can be omitted in the case of narrow string literals since sizeof(char) is always 1, but I included it for completeness.

You can also do it yourself using a loop, and I definitely recommend trying to do so when you have time. It does a simple shallow copy from the source to the destination, taking care to be careful about overlapping memory ranges. Consult any decent C reference for more information on the function since it is a part of the standard C library.

Comments

0

Unfortunately, you cannot just pop that spot out in C. If you were using a linked list, like you said, you would be able to.

It looks like you have an error in your code. Because you are incrementing j and decrementing size_array, you wind up not touching of the elements beyond size_array/2+1.

It also looks like you're using code beyond this that you haven't posted, which makes it confusing to debug, but the code you want looks approximately like this:

for(j=0;j<size_array-1;j++){
    if(strcmp(a[j],input)==0){
       strcpy(a[j], a[j+1]);
}

Note that size_array is one smaller, because you don't want to check the end of the list because there is nothing you can do about it. Also, for strings, you cannot use the '=' operator. You must use strcpy or do it manually.

Comments

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.