【问题标题】:Insert structure arrays dynamic in C. Exited, segmentation fault在 C 中动态插入结构数组。退出,分段错误
【发布时间】:2019-07-26 04:21:46
【问题描述】:

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

无法将值插入动态数组,响应“退出,分段错误”。

谁能帮帮我,这是个问题。 谢谢 ..................................................... ..................................................... ..................................................... ..................................................... ..................................................... ..................................................... ......

语法:

#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

【问题讨论】:

  • dataItems 未初始化到任何内存位置,可以位于内存中的任何位置,分配“动态数组”,使用Item dataItems[storage]Item *dataItems = malloc(sizeof(Item) * storage) 并在使用前检查dataItem
  • 你不能做char* name;...scanf("%s", name);,因为你还没有为名字创建任何内存——事实上你还没有初始化指向任何东西的指针。这与@dvhh 提到的问题相同。基本上你需要初始化所有的变量。

标签: c arrays structure


【解决方案1】:

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

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

  1. 您尚未初始化此指针,它指向某处。因此,表达式 &amp;dataItems[currentStorageAmount] 会为您提供内存中的随机位置,这会导致您出现分段错误。
  2. 表达式sizeof(dataItems) / sizeof(dataItems[0]) 给您的结果与您的预期不同:指针大小除以结构大小。换句话说,它是零。
  3. 您需要在使用dataItems之前分配内存。

【讨论】:

    【解决方案2】:

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

    scanf("%s", name);
    

    您已经声明了一个指针 char* 名称,但您还没有分配它的堆内存。正确的解决方法是换行,

    char *name
    

    char* name = malloc(128);
    

    然后,代码正在运行。

    【讨论】:

      猜你喜欢
      • 2021-04-27
      • 2021-07-17
      • 2020-11-08
      • 1970-01-01
      • 1970-01-01
      • 2021-08-11
      • 2015-06-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多