0

I am trying to remove an item from a list. I am lost to how I would do that with memset and memmove. Example of how I want it:

Initial array:

  1. test1 1 kg
  2. test2 2 kg
  3. test3 3 kg
  4. test4 4 kg
  5. test5 5 kg

After removing item 2:

  1. test1 1 kg
  2. test3 3 kg
  3. test4 4 kg
  4. test5 5 kg

My code:

#define NameLength 20
#define UnitLenght 10
#define SpaceName 5

struct object
{
    char name[NameLength];
    float amount;
    char unit[UnitLenght];
};
struct inventory
{
    struct object *add;
    int nrOfobject;
};

void allocateMemory(struct inventory *allItem);
void addItems(struct inventory *allItem);//Adding an item makes nrOfobject +1
void removeOneItem(struct inventory *allItem);//based on the answer from @Bodo

int main(void)
{
    struct inventory shopping = {NULL, 0};
    int choice;
    printf("\nWelcome to the shopping list manager.\n");
    do
    {
        printf("\n1 - add grocery");
        printf("\n2 - remove grocery from list");
        scanf("%d", &choice);
        while(getchar() != '\n');

        switch (choice)
        {
        case 1:
            allocateMemory(&shopping);
            if (shopping.add == NULL)
                break;
            addItems(&shopping);
            break;

        case 2:
            removeOneItem(&shopping);
            break;

void allocateMemory(struct inventory *allItem)
{
    struct object *tempurary;
    if (allItem->nrOfobject == 0)
        tempurary = (struct object *)calloc(1, sizeof(*tempurary));
    else
        tempurary = (struct object *)realloc(allItem->add, sizeof(*tempurary)*(allItem->nrOfobject +1));

    allItem->add = tempurary;

}

void removeOneItem(struct inventory *allItem) 
{
    int t;
    printf("\nWhich item do you want to remove? ");
    scanf("%d", &t);

        memmove(&allItem->add[t-1], &allItem->add[t], (allItem->nrOfobject-t)*sizeof(*allItem->add));
        allItem->nrOfobject--;
        struct object *tempurary;
        tempurary = realloc(allItem->add, sizeof(*tempurary)*(allItem->nrOfobject));
        allItem->add = tempurary;
    
}

Added the code, minor changes to the names, and how I reconstructed my code, nrOfItemsAddedis the same as nrOfobject. Adding to the question as this was a very old question. I wasn't looking for how to do it in code, particularly how I would apply the concept. The answer from @Bodo is exactly what I wanted, just some hints and help with the logic.

zellez
  • 398
  • 2
  • 17
  • 1
    you do not have the code for removing from the list. What have you tried for this so far? – Rishikesh Raje Jan 08 '20 at 07:18
  • 1
    Note, you [should not cast the result of `malloc()`](https://stackoverflow.com/a/605858/1270789). Nor should you [`fflush(stdin)`](https://stackoverflow.com/q/28955590/1270789). – Ken Y-N Jan 08 '20 at 08:00
  • Please [edit] your code to reduce it to a [mcve] of your problem. Your current code includes much that is peripheral to your problem - a minimal sample normally looks similar to a good unit test: only performing one task, with input values specified for reproducibility. – Toby Speight Jan 08 '20 at 09:15
  • @RishikeshRaje: Oh, stop. Having existing code is needed for debugging. For problems of design, not debugging, having existing code is not a prerequisite. It would be nice to have code illustrating what sort of objects and list OP has (which I see in the history was present at the time of your comment), but they certainly do not need to have code for removing items from the list in order to ask how to remove items from the list. It is a question about concepts, not about debugging existing code. – Eric Postpischil Mar 18 '20 at 14:12
  • I just noticed that you removed the code instead of simplifying it. Of course other users are right that your code contains large parts that are not directly related to the problem of removing an item. The removal of the code makes my answer more difficult to understand because it contains references to your code. – Bodo May 14 '21 at 09:42
  • It doesn't make sense to change a question after an answer has been written in a way that the answer does no longer match the modified question. The code you added now does not fully make clear how your data is organized, and it even contains the function `removeOneItem` which is the solution you are asking about. – Bodo May 15 '21 at 11:41

1 Answers1

3

This answer is intended as a hint, not a full solution.

If you want to remove item 2 you can simply move the data from the address of item 3 with the size of 3 items (3..5) to the address of item 2 using memmove.

After this decrement nrOfItemsAdded.

Optionally you can realloc() the memory to the lower size.

If you don't decrease the allocated size, you can optionally use memset to clear the memory of 1 element at the (old) address of item 5. This is not really necessary as nrOfItemsAdded tells you that this element is unused and the memory will be overwritten in the next call to addItems.


Edit:

This answer contains references to functions and variables in the code that was shown in the original version of the question. Unfortunately the code was removed completely instead of creating a minimal reproducible example.
Now some incomplete code has been added that even contains the solution based on this answer. To understand this answer it is recommended to refer to the original code.

Bodo
  • 9,287
  • 1
  • 13
  • 29