在C中动态插入结构数组。退出,分段错误

时间:2019-07-26 04:21:46

标签: c arrays structure

我对结构数组有问题,无法插入动态值。

无法将值插入动态数组中,响应``已退出,分段错误''。

谁能帮我,那是问题。 谢谢 ................................................... ................................................... ................................................... ................................................... ................................................... ................................................... ................

语法:

#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
#define FLUSH while (getchar() != '\n') 

typedef struct{
    char *Name;
    int Qty;
} Item;

static void insert(Item* i, char *name, int qty)
{
    i->Name = name;
    i->Qty = qty;
}

int main() {

    int storage = 0, menu;
    printf("Temporary Storage\n");

    //Input storage amount
    bool isInputStorage = true;
    while (isInputStorage) {
        printf("Input Storage amount [1..10]: ");
        scanf("%d", &storage);

        if (storage <= 10 && storage >= 1) {
            isInputStorage = false;
        }
        else {
            printf("\n[!]Please enter numbers [1..10] for Storage amount.\n\n");
        }
        FLUSH;
    }

    Item *dataItems;

    //Input Menu
    bool isInputMenu = true;
    while (isInputMenu) {

        printf("\n\nMenu\n");
        printf("=============\n");
        printf("1. Add items\n");
        printf("4. Exit\n");
        printf("Choose Menu [1..4]: ");
        scanf("%d", &menu);

        if (menu >= 1 && menu <= 4) {

            if (menu == 1) {
                char* name;
                int qty;

                //Insert to arrays storage
                int currentStorageAmount = sizeof(dataItems) / sizeof(dataItems[0]);
                if (currentStorageAmount >= storage) {
                    printf("Storage is Full");
                }
                else {

                    printf("Input name of Item : ");
                    scanf("%s", name);

                    bool isQty = true;
                    while (isQty) {
                        FLUSH;
                        printf("Input qty of Item : ");

                        int correctQty = scanf("%d", &qty);

                        if (correctQty == 1) {
                            isQty = false;
                        }
                        else {
                            printf("\n[!]Please enter number for Qty!\n\n");
                        }

                    }

                    //action to insert
                    insert(&dataItems[currentStorageAmount], name, qty);

                }
            }
            else if (menu == 4) {
                printf("\nThank you for using this application.\n");
                isInputMenu = false;
            }
        }
        else {
            printf("\n[!]Please enter numbers [1..4] for choose Menu.");
        }

        menu = 0;
        FLUSH;
    }

    system("pause");
    return 0;
}

结果:

Temporary Storage
Input Storage amount [1..10]: 4

Menu
=============
1. Add items
4. Exit
Choose Menu [1..4]: 1
Input name of Item : test
Input qty of Item : 5
exited, segmentation fault

2 个答案:

答案 0 :(得分:2)

您正在询问数组,但是代码中没有一个数组...

您真正拥有的是Item的指针。这意味着三件事:

  1. 您尚未初始化此指针,它指向 somewhere 。因此,表达式&dataItems[currentStorageAmount]在内存中为您提供了一个随机位置,从而导致您出现分段错误。
  2. 表达式sizeof(dataItems) / sizeof(dataItems[0])给您的东西与您期望的有所不同:指针的大小除以结构的大小。换句话说,它是零。
  3. 您需要先使用dataItems分配内存。

答案 1 :(得分:0)

我用GDB检查代码,错误在第65行,

scanf("%s", name);

您已声明指针char *名称,但尚未分配其堆内存。正确的解决方案是更改线路,

char *name

收件人

char* name = malloc(128);

然后,代码正在运行。