I'm trying to implement a linked list in c. I get input from user, put it in a struct called Box, and save the order of the inputs using a linked list. here is the struct :
struct Box
{
struct Box *previous;
struct Box *next;
int amount;
};
and here is the implementation :
void main()
{
struct Box firstBox;
scanf("%d", &(firstBox.amount));
struct Box *addressKeeper;
addressKeeper = &firstBox;
for (int i = 0; i < 3; i++)
{
struct Box newBox;
scanf("%d", &(newBox.amount));
newBox.previous = addressKeeper;
addressKeeper->next = &newBox;
addressKeeper = &newBox;
}
}
but when I print the next boxes' addresses in this way, all of them are the same?
struct Box *ptr = &firstBox;
for (int i = 0; i < 3; i++)
{
printf("%p \n", ptr->next);
ptr = ptr->next;
}
am I doing something wrong?