Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

expected '=', ',', ';', 'asm' or '__attribute__' before '.' token

Tags:

c

enter image description hereCould not able to solve this.. I am implementing a queue. After writing the complete code I had the error listed below:

expected '=', ',', ';', 'asm' or '__attribute__' before '.' token

Then I wrote a simple program, but same problem persists. Couldn't able to understand how to solve this. I have looked into solutions in stackoverflow.com and google.com a lot but still couldn't able to solve this.Please help.

I would like to initialize globally Q.front = Q.rear = Any value

#include <stdio.h>
#include <stdlib.h>
struct Queue
{
    int front, rear;
    int queue[10] ;
};
struct Queue Q;
Q.front = 0;
Q.rear = 0;

int main()
{
    return 0;
}
like image 796
Rasmi Ranjan Nayak Avatar asked Apr 25 '12 07:04

Rasmi Ranjan Nayak


3 Answers

Q.front = 0; is not a simple initializer, it is executable code; it cannot occur outside of a function. Use a proper initializer for Q.

struct Queue Q = {0, 0};

or with named initializer syntax (not available in all compilers, and as yet only in C):

struct Queue Q = {.front = 0, .rear = 0};
like image 180
geekosaur Avatar answered Nov 16 '22 07:11

geekosaur


You can't initialize variable using Q.front = 0; Q.rear = 0; in global scope. Those statements should be inside main in your case.

like image 39
Naveen Avatar answered Nov 16 '22 07:11

Naveen


As @Naveen said you can't assign to a member of a struct that is in global scope. Depending on the version of C though you could do this:

struct Queue q = {0,0};

or

struct Queue q = {.front = 0, .rear = 0 };
like image 30
Will Avatar answered Nov 16 '22 08:11

Will