gpt4 book ai didi

c - 释放结构数组的元素?

转载 作者:太空宇宙 更新时间:2023-11-04 07:56:43 24 4
gpt4 key购买 nike

该程序用于创建项目库存、查看库存并允许用户通过项目编号删除项目。一切都通过一个结构数组,在删除库存项目后,我很难释放数组元素,方法是通过一个函数将其上方的所有其他内容移动到数组中。任何帮助表示赞赏。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_INVENTORY_SIZE 100

typedef struct {
char item_Number[4];
char item_Name[20];
float item_Profit;
float latest_Price;
float selling_Price;
unsigned int stock;
unsigned int total_Sold;
}InventoryItemType;

void MainMenu();
void displayInventory(InventoryItemType *, int);
void displaySales(InventoryItemType *, int);
InventoryItemType *addItem(void);
InventoryItemType deleteItem(InventoryItemType *, int);

int main()
{
int i=0, item_count=0;
char selection, code[4];
InventoryItemType inventoryItems[MAX_INVENTORY_SIZE];
while(1)
{
MainMenu();

scanf(" %c", &selection);
switch(selection)
{
case 'A' :
displayInventory(inventoryItems, item_count);
system("pause");
system("cls");
continue;
case 'B' :
displaySales(inventoryItems, item_count);
system("pause");
system("cls");
continue;
case 'C' :
if(item_count == MAX_INVENTORY_SIZE - 1)
{
printf("Array is full\n");
system("pause");
continue;
}
inventoryItems[item_count] = *addItem();
item_count++;
continue;
case 'D' :
*inventoryItems=deleteItem(inventoryItems, item_count);
free(&inventoryItems[item_count]);
item_count--;
continue;
case 'E' :
case 'F' :
case 'G' :
case 'H' :
default :
printf("Invalid Entry\n" );
system("pause");
}
system("cls");
}
}
void MainMenu()
{
printf("A. Display Inventory\n");
printf("B. Display Sales\n");
printf("C. Add Item\n");
printf("D. Remove Item\n");
printf("E. Enter Shipment\n");
printf("F. Update Sales\n");
printf("G. Sort\n");
printf("H. Exit\n");
printf("Make a selection\n");
}
void displayInventory(InventoryItemType *display, int key)
{
system("cls");
{
int i;
for(i=0; i<key; i++)
{
printf("Item No.:%s\n", display[i].item_Number);
printf("Item Name:%s\n", display[i].item_Name);
printf("Item Stock:%d\n",display[i].stock);
printf("Item Purchased Price:%.2f\n", display[i].latest_Price);
printf("Total Value of Items:%.2f\n", (display[i].stock)*(display[i].latest_Price));
printf("\n");
}
}
}
void displaySales(InventoryItemType *display, int key)
{

int i;
float total_profit=0;
system("cls");
for(i=0; i<key; i++)
{
printf("Item No.:%s\n", display[i].item_Number);
printf("Item Name:%s\n", display[i].item_Name);
printf("Number of Item Sold:%d\n", display[i].total_Sold);
printf("Item Selling Price:%.2f\n", display[i].selling_Price);
printf("Total Profit from Item:%.2f\n", (display[i].selling_Price-display[i].latest_Price)*display[i].total_Sold);
total_profit=total_profit+((display[i].selling_Price-display[i].latest_Price)*display[i].total_Sold);
if(i==key-1)
printf("\nTotal Over-all Profit:%.2f", total_profit);
printf("\n\n");
}
}
InventoryItemType *addItem(void)
{
InventoryItemType *current = (InventoryItemType*) malloc (sizeof *current);
system("cls");
if(current == NULL)
return NULL;
printf("\nEnter details of item \n\n");
printf("Enter Item no: \n");
scanf("%s", current->item_Number);
printf("Enter Item Name: \n");
scanf("%s", current->item_Name);
printf("Enter Stock: \n");
scanf("%d", &current->stock);
printf("Enter Purchase Price: \n");
scanf("%f", &current->latest_Price);
current->selling_Price=(current->latest_Price)*1.5;
current->total_Sold=0;
system("cls");
return current;
}
InventoryItemType deleteItem (InventoryItemType *deleted, int item_count)
{
char code[4];
int i;
printf("Enter Item Number to be Deleted\n");
scanf("%3s", code);
for(i=0;i<item_count;i++)
{
if(strcmp(code,deleted[i].item_Number)==0)
break;
}
for(;i<item_count; i++)
deleted[i]=deleted[i+1];
return *deleted;
}

在@Barmar 的帮助下进行编辑:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_INVENTORY_SIZE 100

typedef struct {
char item_Number[4];
char item_Name[20];
float item_Profit;
float latest_Price;
float selling_Price;
unsigned int stock;
unsigned int total_Sold;
}InventoryItemType;
void MainMenu();
void displayInventory(InventoryItemType *[], int);
void displaySales(InventoryItemType *[], int);
InventoryItemType *addItem(void);
InventoryItemType deleteItem(InventoryItemType *, int);

int main()
{
int i=0, item_count=0;
char selection, code[4];
InventoryItemType *inventoryItems[MAX_INVENTORY_SIZE];
while(1)
{
MainMenu();

scanf(" %c", &selection);
switch(selection)
{
case 'A' :
displayInventory(inventoryItems, item_count);
system("pause");
system("cls");
continue;
case 'B' :
displaySales(inventoryItems, item_count);
system("pause");
system("cls");
continue;
case 'C' :
if(item_count == MAX_INVENTORY_SIZE - 1)
{
printf("Array is full\n");
system("pause");
continue;
}
inventoryItems[item_count] = addItem();
item_count++;
continue;
case 'D' :
**inventoryItems=deleteItem(*inventoryItems, item_count);
item_count--;
continue;
case 'E' :
case 'F' :
case 'G' :
case 'H' :
default :
printf("Invalid Entry\n" );
system("pause");
}
system("cls");
}
}
void MainMenu()
{
printf("A. Display Inventory\n");
printf("B. Display Sales\n");
printf("C. Add Item\n");
printf("D. Remove Item\n");
printf("E. Enter Shipment\n");
printf("F. Update Sales\n");
printf("G. Sort\n");
printf("H. Exit\n");
printf("Make a selection\n");
}
void displayInventory(InventoryItemType *display[], int key)
{
system("cls");
{
int i;
for(i=0; i<key; i++)
{
printf("Item No.:%s\n", display[i]->item_Number);
printf("Item Name:%s\n", display[i]->item_Name);
printf("Item Stock:%d\n",display[i]->stock);
printf("Item Purchased Price:%.2f\n", display[i]->latest_Price);
printf("Total Value of Items:%.2f\n", (display[i]->stock)*(display[i]->latest_Price));
printf("\n");
}
}
}
void displaySales(InventoryItemType *display[], int key)
{

int i;
float total_profit=0;
system("cls");
for(i=0; i<key; i++)
{
printf("Item No.:%s\n", display[i]->item_Number);
printf("Item Name:%s\n", display[i]->item_Name);
printf("Number of Item Sold:%d\n", display[i]->total_Sold);
printf("Item Selling Price:%.2f\n", display[i]->selling_Price);
printf("Total Profit from Item:%.2f\n", (display[i]->selling_Price-display[i]->latest_Price)*display[i]->total_Sold);
total_profit=total_profit+((display[i]->selling_Price-display[i]->latest_Price)*display[i]->total_Sold);
if(i==key-1)
printf("\nTotal Over-all Profit:%.2f", total_profit);
printf("\n\n");
}
}
InventoryItemType *addItem(void)
{
InventoryItemType *current = (InventoryItemType*) malloc (sizeof *current);
system("cls");
if(current == NULL)
return NULL;
printf("\nEnter details of item \n\n");
printf("Enter Item no: \n");
scanf("%s", current->item_Number);
printf("Enter Item Name: \n");
scanf("%s", current->item_Name);
printf("Enter Stock: \n");
scanf("%d", &current->stock);
printf("Enter Purchase Price: \n");
scanf("%f", &current->latest_Price);
current->selling_Price=(current->latest_Price)*1.5;
current->total_Sold=0;
system("cls");
return current;
}
InventoryItemType deleteItem (InventoryItemType *deleted, int item_count)
{
char code[4];
int i;
printf("Enter Item Number to be Deleted\n");
scanf("%3s", code);
for(i=0;i<item_count;i++)
{
if(strcmp(code,deleted[i].item_Number)==0)
break;
}
free(deleted[i]);
for(;i<item_count; i++)
deleted[i]=deleted[i+1];
return *deleted;
}

最佳答案

inventoryItems 的元素不是动态分配的,因此您无法释放它们。它们直接包含在数组中。当你这样做时:

inventoryItems[item_count] = *add_item();

您正在取消引用指针,而不是将指针存储在数组中。这会复制动态分配的结构,并丢弃其指针(因此会发生内存泄漏)。

您应该更改此数组的类型以包含指针:

InventoryItemType * inventoryItems[MAX_INVENTORY_SIZE];

那么你不应该解引用 addItem 返回的指针,你应该直接分配它:

inventoryItems[item_count] = add_item();

然后你释放它:

free(inventoryItems[item_count]);

并且在 displayInventory() 中,您将所有 display[i].xxx 更改为 display[i]->xxx

关于c - 释放结构数组的元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49578288/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com