reswanth
reswanth

Reputation: 73

creating an array of structures and making them as linked lists and traversing them using c

In this program I created a array(node[3]) of structure(struct eg) and made them as linked lists while trying to print the elements of the list I am getting only 1 output that is 3

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

struct eg
{
    struct eg *next;
    int age;
}node[3];

main()
{
    struct eg *head,*temp;
    int i;

    head=temp=node;
    for(i=1;i<3;i++)
    {
       temp->age=i;
       temp->next=temp+i;
    }
    temp->next=0;
    temp->age=3;
    while(head!=0)
    {
       printf("%d",head->age);
       head=head->next;
    } 
}

Upvotes: 0

Views: 36

Answers (1)

dbush
dbush

Reputation: 224127

temp->next=temp++;

You're reading and modifying temp in a single expression with no sequence point. This invokes undefined behavior.

You need to separate the increment from the assignment:

temp->next=temp+1;
temp++;

Upvotes: 2

Related Questions