0

I have a problem with structure arrays, to insert dynamic values.

Cannot insert values ​​into dynamic arrays, response "Exited, segmentation fault".

Can anyone help me, that's problem. Thank you ............................................................................................................................................................................................................................................................................................................................

Syntax:

#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;
}

Result:

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
  • 1
    dataItems is not initialized to any memory location and could be located anywhere in memory, allocate the "dynamic array", either using Item dataItems[storage] or Item *dataItems = malloc(sizeof(Item) * storage) and check dataItem before using. Commented Jul 26, 2019 at 4:56
  • 1
    You can't do char* name;...scanf("%s", name); because you haven't created any memory for the name to go - in fact you haven't initialized the pointer to ANYTHING. It's the same problem that @dvhh mentioned. Basically you need to initialize ALL your variables. Commented Jul 26, 2019 at 5:46

2 Answers 2

3

You are asking about arrays but there is no one array in your code...

What you really have is the pointer to Item. This means three things:

  1. You have not initialized this pointer, and it points to somewhere. So the expression &dataItems[currentStorageAmount] gives you a random position in memory that leads you to the segmentation fault.
  2. The expression sizeof(dataItems) / sizeof(dataItems[0]) gives you something different from what you expect: size of the pointer divided to size of the structure. In other words it is zero.
  3. You need to allocate memory before using the dataItems.
Sign up to request clarification or add additional context in comments.

Comments

0

I check the code with GDB, the error is at line 65,

scanf("%s", name);

you have declared a pointer char* name, but you have not allocated its heap memory yet. The correct solution is to change the line,

char *name

To

char* name = malloc(128);

Then, the code is running.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.