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:
- test1 1 kg
- test2 2 kg
- test3 3 kg
- test4 4 kg
- test5 5 kg
After removing item 2:
- test1 1 kg
- test3 3 kg
- test4 4 kg
- 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, nrOfItemsAdded
is 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.