Thread: FIFO Queue in C (unable to detect the error reason)

  1. #1
    Registered User
    Join Date
    Oct 2014
    Posts
    7

    FIFO Queue in C (unable to detect the error reason)

    Hi, I follow one article's help to write a c code that is to make FIFO Circular queue. During Compilation I am getting the following error:
    ERROR:
    fifo1.c: In function ‘enqueue’:
    fifo1.c:43:14: error: ‘rear’ undeclared (first use in this function)
    q->items[rear] = x;
    ^
    fifo1.c:43:14: note: each undeclared identifier is reported only once for each function it appears in
    fifo1.c: In function ‘dequeue’:
    fifo1.c:50:18: error: ‘front’ undeclared (first use in this function)
    x = q->items[front];
    ^
    ************************************
    My code is:
    ************************************
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define max 10
    
    
    typedef struct fifo_q{
       int count;
       int front;
       int rear;
       int items[max];
    };
    
    
    void initialize (struct fifo_q *q);
    int isEmpty(struct fifo_q q);
    int isFull(struct fifo_q q);
    void enqueue(struct fifo_q *q, int x);
    int dequeue(struct fifo_q *q);
    
    
    
    
    void initialize (struct fifo_q *q)
    {
        q->count = 0;
        q->front = 0;
        q->rear = -1;
    }
    
    
    int isEmpty(struct fifo_q q)
    {
        return (q.count == 0);
    }
    
    
    int isFull(struct fifo_q q)
    {
        return (q.count == max);
    }
    
    
    
    
    void enqueue(struct fifo_q *q, int x){
    if (q->count == max){
        printf( "%d is not inserted. Queue is full.\n", x);
    }
    else{
        q->count = (q->count + 1);
        q->rear = (q->rear + 1) % max;
        q->items[rear] = x;
        }
    }
    
    
    int dequeue(struct fifo_q *q){
        int x;
        q->count = q->count - 1;
        x = q->items[front];
        q->front = (q->front + 1)% max;
        return x;
        }
    
    
    int main(){
        struct fifo_q fifo1;
        int n;
        initialize(&fifo1);
       // Just to check I am calling the Enqueue function  
        printf("Enter your data \n");
        scanf("%d", &n);
        enqueue(&fifo1, n);
        ......
        ..... 
        return 0;
    }
    Please anyone give the reason why I am getting this declaration related error.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Look closely at the following snippet:

    Code:
    void enqueue(struct fifo_q *q, int x)
    {
       if (q->count == max){
          printf( "%d is not inserted. Queue is full.\n", x);
       }
       else{
          q->count = (q->count + 1);
           q->rear = (q->rear + 1) % max;
           q->items[rear] = x; // <<<<<<<<<<<<
       }
    }
    Look at that highlighted line. Where has "rear" been defined?

    Also you need to be consistent about your indentation, it'll make reading your program much much easier.


    Jim

  3. #3
    Registered User
    Join Date
    Oct 2014
    Posts
    7
    Hello Jim,
    Thanks for the quick help. I am still confused about that code.
    Because, as compiler was saying variable is not declared but the variable "rear" is struct data member, so why should I re-declare it again.
    And it is a int type so in items[rear], rear will be replaced by its value.
    Actually, I changed the code in following ..
    Code:
    int r;
    void enqueue(struct fifo_q *q, int x)
    {
         if (q->count == max){
          printf( "%d is not inserted. Queue is full.\n", x);
     }
    else{
            q->count = (q->count + 1);
            q->rear = (q->rear + 1) % max;
            r = q->rear;
            q->items[r] = x;
           }
    }

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Because, as compiler was saying variable is not declared but the variable "rear" is struct data member, so why should I re-declare it again.
    Code:
           q->rear = (q->rear + 1) % max; // q->rear is a structure member.
           q->items[rear] = x; // <<<<<<<<<<<< rear is not a structure member.
    Because in the second line above rear is not a member of any structure, look at the previous line where rear is a member of a structure, notice the difference.

    Code:
    void enqueue(struct fifo_q *q, int x)
    {
         if (q->count == max){
          printf( "%d is not inserted. Queue is full.\n", x);
     }
    else{
            q->count = (q->count + 1);
            q->rear = (q->rear + 1) % max;
            q->items[q->rear] = x;  
           }
    }
    Jim

  5. #5
    Registered User
    Join Date
    Oct 2014
    Posts
    7
    Thanks a lot Jim...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. FIFO queue vs Link List
    By DeeMan in forum C Programming
    Replies: 8
    Last Post: 02-16-2013, 08:29 PM
  2. Priority queue and FIFO
    By Elysia in forum C++ Programming
    Replies: 12
    Last Post: 02-29-2012, 02:32 PM
  3. Help with FIFO queue
    By Martin_T in forum C Programming
    Replies: 10
    Last Post: 11-08-2009, 10:30 AM
  4. understanding queue FIFO
    By arastoo.s in forum C Programming
    Replies: 6
    Last Post: 08-18-2009, 10:16 AM
  5. Help with FIFO QUEUE
    By jackfraust in forum C++ Programming
    Replies: 23
    Last Post: 04-03-2009, 08:17 AM

Tags for this Thread