Search code examples
cqueue

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


I am implementing a queue, where 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;
}

But I get this error:

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

enter image description here


Solution

  • 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};