Dynamic data structure with scanf

1.8k views Asked by At

I have a very basic question, thanks for your patience.

I have a dynamic data structure with just an integer value and a pointer to the next structure. I am using scanf to get the user input to get 5 values to add to the structure and trying to print the output at the end. I am having trouble with the syntax to get the input into the structure. I have looked around StackOverflow and Google, with no avail (probably because it is too basic!)

here is the code:

#include <stdio.h>

struct List
{
    int value;
    struct List *nextaddr;
};

int main()
{
    int int1, int2, int3, int4, int5;

    printf("please enter the first integer: ");
    scanf("%d", int1);
    struct List t1 = {int1};

    printf("please enter the second integer: ");
    scanf("%d", int2);
    struct List t2 = {int2};

    printf("please enter the third integer: ");
    scanf("%d", int3);
    struct List t3 = {int3};

    printf("please enter the fourth integer: ");
    scanf("%d", int4);
    struct List t4 = {int4};

    printf("please enter the fifth integer: ");
    scanf("%d", int5);
    struct List t5 = {int5};

    struct List *first;

    first = &t1;
    t1.nextaddr = &t2;
    t2.nextaddr = &t3;
    t3.nextaddr = &t4;
    t4.nextaddr = &t5;
    t5.nextaddr = NULL;

    printf("%i\n%i\n%i\n%i\n%i\n",first->value,t1.nextaddr->value,t2.nextaddr->value,t3.nextaddr->value,t4.nextaddr->value);

    return 0;
}

How can I get the user input into the structure?

2

There are 2 answers

1
Itay Karo On BEST ANSWER

scanf should get the address of the integer as in: scanf("%d", &int1);

1
templatetypedef On

A few things-

Typically, when working with a linked structure like a linked list (as you are in this case), you allocate the memory for the objects on the heap using malloc rather than on the stack. The rationale behind this is that you want the elements of a linked structure to outlive the function that creates them, and so stack-allocating the individual cells isn't likely to work correctly. You probably want to create the structure by writing something like

struct List* entry = malloc(sizeof(struct List));

As a follow-up, since the logic for reading the contents of a linked list cell from the user and adding it to the list is identical for each cell you're reading, you probably don't want to just copy the code for it five times. Instead, consider writing a function like this:

struct List* ReadListEntry(void) {
    struct List* entry = malloc(sizeof(struct List));
    /* ... initialize 'entry' ... */

    return entry;
}

That way, your code in main can be five times shorter, and if you find any bugs in the code (as you seem to have done), you only need to change it once, not five times.

As for your original question, I think that the problem is that you're writing

scanf("%d", myValue);

instead of

scanf("%d", &myValue);

This first version is incorrect and likely to cause a crash at runtime. scanf assumes that you're providing a pointer to an integer, not an integer, as the argument whenever you use the %d format specifier, so the explicit ampersand is probably what's hurting you.

Combining this with the above idea to use a helper function to produce heap-allocated list cells, you might want to try writing a function like this:

struct List* ReadListEntry(void) {
    struct List* entry = malloc(sizeof(struct List));

    scanf("%d", &entry->value);
    entry->nextaddr = NULL;

    return entry;
}

Given this, you can probably rewrite your main function to be much simpler than what you have now. I'll leave that as an exercise to the reader. :-)

Hope this helps!