Rohit Goel
Rohit Goel

Reputation: 63

error: expected expression before '{' token|

  struct node{
        int data;
        struct node *next;
    };
main(){
    struct node a,b,c,d;
    struct node *s=&a;

    a={10,&b};
    b={10,&c};
    c={10,&d};
    d={10,NULL};


    do{
        printf("%d %d",(*s).data,(*s).next);

        s=*s.next;
    }while(*s.next!=NULL);

}

It is showing error at a={10,&b};Expression syntex error.please help.. thankzz in advance

Upvotes: 6

Views: 6593

Answers (2)

Sourav Ghosh
Sourav Ghosh

Reputation: 134286

Initialization of a struct variable using only bracket enclosed list is allowed at definition time. use

struct node a={10,&b};

Otherwise, you've to use a compound literal [on and above c99]

a=(struct node){10,&b};

Upvotes: 6

reader
reader

Reputation: 516

Initialize the variable immediately:

struct node a={10,NULL};

And then assign the address:

a.next = &b;

Or use a compound literal:

a=( struct node ){10,&b};  //must be using at least C99

Upvotes: 6

Related Questions