Search code examples
cdata-structuresinputlinked-listdynamic-data

Dynamic data structure with scanf


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?


Solution

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