Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 881
Rohit Goel Avatar asked Jan 29 '15 16:01

Rohit Goel


2 Answers

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
like image 53
reader Avatar answered Oct 16 '22 11:10

reader


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};
like image 40
Sourav Ghosh Avatar answered Oct 16 '22 11:10

Sourav Ghosh