C 我的指针和结构代码正在显示垃圾值

C 我的指针和结构代码正在显示垃圾值,c,C,目前,我正在尝试创建一个程序,对一家商店的产品进行盘点。我已经能够创建一个函数,允许用户在结构数组中输入新项,但是当我尝试打印值时,我会得到垃圾值。(请忽略switch语句,因为代码正在进行中) #包括 #包括 #定义最大库存大小100 类型定义结构{ 字符项_编号[3]; 字符项_名称[20]; 浮动项目利润; 浮动最新价格; 无符号整数股票; 未签名整数总销售额; 结构InventoryItemType*下一步; }清单项目类型; void main菜单(); 作废显示库存(Inventor

目前,我正在尝试创建一个程序,对一家商店的产品进行盘点。我已经能够创建一个函数,允许用户在结构数组中输入新项,但是当我尝试打印值时,我会得到垃圾值。(请忽略switch语句,因为代码正在进行中)

#包括
#包括
#定义最大库存大小100
类型定义结构{
字符项_编号[3];
字符项_名称[20];
浮动项目利润;
浮动最新价格;
无符号整数股票;
未签名整数总销售额;
结构InventoryItemType*下一步;
}清单项目类型;
void main菜单();
作废显示库存(InventoryItemType*(*));
无效附加项(InventoryItemType,int i);
int main()
{
int i=1;
字符选择;
整数计数=1;
InventoryItemType*inventoryItems[最大库存大小];
inventoryItems[0]=空;
而(1)
{
主菜单();
scanf(“%c”,选择(&S);
开关(选择)
{
案例“A”:
显示库存(库存项目);
打破
案例“B”:
案例“C”:
inventoryItems[count]=(InventoryItemType*)malloc(sizeof(InventoryItemType));
addItem(*存货项目[计数],计数);
继续;
案例“D”:
案例“E”:
案例“F”:
案例“G”:
案例“H”:
违约:
printf(“无效\n”);
}
printf(“代码底部”);
系统(“暂停”);
}
}
void主菜单()
{
printf(“A.显示库存\n”);
printf(“B.显示销售”\n);
printf(“C.添加项目\n”);
printf(“D.删除项目\n”);
printf(“E.输入装运\n”);
printf(“F.更新销售信息”);
printf(“G.Sort\n”);
printf(“H.退出”\n);
printf(“进行选择”);
}
无效显示库存(InventoryItemType*显示)
{
int i;

对于(i=0;i有许多错误/冲突。太多了,无法单独评论。我已经注释并更正了代码,显示了错误和之前/之后。还有更多的工作要做,但这应该会让您更接近

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

typedef struct {
    char item_Number[3];
    char item_Name[20];
    float item_Profit;
    float latest_Price;
    unsigned int stock;
    unsigned int total_Sold;
    struct InventoryItemType *next;
} InventoryItemType;

void MainMenu();

#if 0
void displayInventory(InventoryItemType *(*));
#else
void displayInventory(InventoryItemType *,int);
#endif

#if 0
void addItem(InventoryItemType, int i);
#else
void addItem(InventoryItemType *);
#endif

int main()
{
    int i=1;
    char selection;

    // NOTE/BUG: starting at 1 will leave item 0 undefined
#if 0
    int count=1;
#else
    int count=0;
#endif

    // better to do this with a stack array than pointers to malloc'ed items
#if 0
    InventoryItemType *inventoryItems[MAX_INVENTORY_SIZE] ;
#else
    InventoryItemType inventoryItems[MAX_INVENTORY_SIZE] ;
#endif

#if 0
    // NOTE/BUG: this will segfault because inventoryItems is undefined here
    inventoryItems[0]=NULL;
#endif

    while (1) {
        MainMenu();
        scanf(" %c", &selection);
        switch(selection) {
        case 'A' :
#if 0
            displayInventory(inventoryItems);
#else
            displayInventory(inventoryItems,count);
#endif
            break;
        case 'B' :
        case 'C' :
#if 0
            inventoryItems[count]= (InventoryItemType*)malloc(sizeof(InventoryItemType));
            addItem(*inventoryItems[count], count);
#else
            addItem(&inventoryItems[count]);
            count += 1;
#endif
            continue;
        case 'D' :
        case 'E' :
        case 'F' :
        case 'G' :
        case 'H' :
        default :
            printf("Invalid\n" );
        }
        printf("Bottom of code\n");
        system("pause");
    }
}

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");
}

#if 0
void displayInventory(InventoryItemType display)
{
    int i;
    for(i=0; i<MAX_INVENTORY_SIZE; i++)
    {
        printf("Name:%s\n", display[i].item_Name);
        printf("Stock:%d\n", display[i].stock);
        printf("Price:%.2f\n", display[i].latest_Price);
        printf("Total Value:%.2f\n", (display[i].stock)*(display[i].latest_Price));
        printf("\n");
    }
}

void addItem(InventoryItemType display)
{

    // NOTE/BUG: because this is passed by _value_ nothing will be retained
    // after function return

    printf("\nEnter details of item \n\n");
    printf("Enter Item Name: \n");
    scanf("%s", display.item_Name);
    printf("\nEnter Item no: \n");
    scanf("%s", display.item_Number);

    printf("\nEnter Stock: \n");
    scanf("%d", &display.stock);

    printf("\nPurchase Price: \n");
    scanf("%f", &display.latest_Price);
}
#else
void displayInventory(InventoryItemType *item,int count)
{
    int i;
    for (i=0; i<count; i++, item++) {
        printf("Name:%s\n", item->item_Name);
        printf("Stock:%d\n", item->stock);
        printf("Price:%.2f\n", item->latest_Price);
        printf("Total Value:%.2f\n", (item->stock)*(item->latest_Price));
        printf("\n");
    }
}

void addItem(InventoryItemType *item)
{

    printf("\nEnter details of item \n\n");

    printf("Enter Item Name: \n");
    scanf("%s", item->item_Name);

    printf("\nEnter Item no: \n");
    scanf("%s", item->item_Number);

    printf("\nEnter Stock: \n");
    scanf("%d", &item->stock);

    printf("\nPurchase Price: \n");
    scanf("%f", &item->latest_Price);
}
#endif
#包括
#包括
#定义最大库存大小100
类型定义结构{
字符项_编号[3];
字符项_名称[20];
浮动项目利润;
浮动最新价格;
无符号整数股票;
未签名整数总销售额;
结构InventoryItemType*下一步;
}清单项目类型;
void main菜单();
#如果0
作废显示库存(InventoryItemType*(*));
#否则
无效显示库存(InventoryItemType*,int);
#恩迪夫
#如果0
无效附加项(InventoryItemType,int i);
#否则
无效附加项(InventoryItemType*);
#恩迪夫
int main()
{
int i=1;
字符选择;
//注意/错误:从1开始将保留未定义的项0
#如果0
整数计数=1;
#否则
整数计数=0;
#恩迪夫
//使用堆栈数组比使用指向malloc'ed项的指针更好
#如果0
InventoryItemType*inventoryItems[最大库存大小];
#否则
InventoryItemType inventoryItems[最大库存大小];
#恩迪夫
#如果0
//注意/错误:这将导致故障,因为此处未定义inventoryItems
inventoryItems[0]=空;
#恩迪夫
而(1){
主菜单();
scanf(“%c”,选择(&S);
开关(选择){
案例“A”:
#如果0
显示库存(库存项目);
#否则
显示库存(库存项目、盘点);
#恩迪夫
打破
案例“B”:
案例“C”:
#如果0
inventoryItems[count]=(InventoryItemType*)malloc(sizeof(InventoryItemType));
addItem(*存货项目[计数],计数);
#否则
附加项(&inventoryItems[计数]);
计数+=1;
#恩迪夫
继续;
案例“D”:
案例“E”:
案例“F”:
案例“G”:
案例“H”:
违约:
printf(“无效\n”);
}
printf(“代码底部”);
系统(“暂停”);
}
}
void主菜单()
{
printf(“A.显示库存\n”);
printf(“B.显示销售”\n);
printf(“C.添加项目\n”);
printf(“D.删除项目\n”);
printf(“E.输入装运\n”);
printf(“F.更新销售信息”);
printf(“G.Sort\n”);
printf(“H.退出”\n);
printf(“进行选择”);
}
#如果0
void displayInventory(InventoryItemType显示)
{
int i;
对于(i=0;istock);
printf(“价格:%.2f\n”,项目->最新价格);
printf(“总价值:%.2f\n”,(项目->库存)*(项目->最新价格);
printf(“\n”);
}
}
无效附加项(InventoryItemType*项)
{
printf(“\n输入项目的详细信息\n\n”);
printf(“输入项目名称:\n”);
扫描(“%s”,项目->项目名称);
printf(“\n输入项目编号:\n”);
扫描(“%s”,项目->项目编号);
printf(“\n输入股票:\n”);
scanf(“%d”,&item->stock);
printf(“\n采购价格:\n”);
scanf(“%f”和项目->最新价格);
}
#恩迪夫

您的代码有很多问题

InventoryItemType *inventoryItems[MAX_INVENTORY_SIZE] ;
这声明了
InventoryItemType
指针。这些指针未初始化,因此您需要为其分配内存 它。你在这里做:

inventoryItems[count]= (InventoryItemType*)malloc(sizeof(InventoryItemType));
  • count
    变量被初始化为1,并且从未更改。因此 为
    inventoryItems[1]
    分配新内存而丢失的时间 上一个
    malloc
    调用的指针,因此它正在泄漏

  • 打印数据结构时,您需要执行以下操作:

    for(i=0; i<MAX_INVENTORY_SIZE; i++)
        {
            printf("Name:%s\n", display[i].item_Name);
            ...
    
    像这样声明
    addItem
    display
    只是原始的副本,所以 更改
    显示
    的值只会影响
    显示
    ,而不会影响原始显示。 所以当你这么做的时候

    addItem(*inventoryItems[count], count);
    
    您正在将副本传递给
    addItem
    ,您只需更改副本,
    inventoryItems[count]
    保持不变。此外,您的函数只接受一个 参数,则传递它两个

    addItem
    应使用指针,以便通过 指向原始文件的指针。函数应如下所示:

    int addItem(InventoryItemType *display);
    {
    
        if(display == NULL)
            return 0;
    
        printf("\nEnter details of item \n\n");
        printf("Enter Item Name: \n");
        if(scanf("%19s", display->item_Name) != 1)
            return 0;
    
        clear_stdin();
    
        printf("\nEnter Item no: \n");
        if(scanf("%2s", display->item_Number) != 1)
            return 0;
    
        clear_stdin();
    
        printf("\nEnter Stock: \n");
        if(scanf("%d", &display->stock) != 1)
            return 0;
    
        printf("\nPurchase Price: \n");
        if(scanf("%f", &display->latest_Price) != 1)
            return 0;
    
    
        return 1;
    }
    
     void clear_stdin(void)
     {
         int c;
         while((c = getchar()) != '\n' && c != EOF);
     }
    
    InventoryItemType *addItem(void)
    {
        InventoryItemType *current = calloc(1, sizeof *current);
        if(current == NULL)
            return NULL;
    
        printf("\nEnter details of item \n\n");
        printf("Enter Item no: \n");
        scanf("%2s", current->item_Number);
    
        clear_stdin();
    
        printf("Enter Item Name: \n");
        scanf("%20s", current->item_Name);
    
        clear_stdin();
    
        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;
    }
    

    int addItem(InventoryItemType *display); { if(display == NULL) return 0; printf("\nEnter details of item \n\n"); printf("Enter Item Name: \n"); if(scanf("%19s", display->item_Name) != 1) return 0; clear_stdin(); printf("\nEnter Item no: \n"); if(scanf("%2s", display->item_Number) != 1) return 0; clear_stdin(); printf("\nEnter Stock: \n"); if(scanf("%d", &display->stock) != 1) return 0; printf("\nPurchase Price: \n"); if(scanf("%f", &display->latest_Price) != 1) return 0; return 1; }

     void clear_stdin(void)
     {
         int c;
         while((c = getchar()) != '\n' && c != EOF);
     }
    
    inventoryItems[item_count]=(InventoryItemType*)malloc(sizeof(InventoryItemType));
    
    InventoryItemType *addItem(void)
    {
        InventoryItemType *current = calloc(1, sizeof *current);
        if(current == NULL)
            return NULL;
    
        printf("\nEnter details of item \n\n");
        printf("Enter Item no: \n");
        scanf("%2s", current->item_Number);
    
        clear_stdin();
    
        printf("Enter Item Name: \n");
        scanf("%20s", current->item_Name);
    
        clear_stdin();
    
        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;
    }
    
    case 'C':
        // checking that you don't step outside of bounds
        if(item_count == MAX_INVENTORY_SIZE - 1)
        {
            fprintf(stderr, "Array is full\n");
            break;
        }
    
        inventoryItems[item_count] = addItem();
        if(inventoryItems[item_count] == NULL)
        {
            fprintf(stderr, "Not enough memory\n");
            break;
        }
    
        item_count++;
        continue;
    
    case 'D':
        ...