0

I'm trying to use a queue in my program, but it won't compile and I don't know why. The relevant part of the code is as follows.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#ifndef CUSTOMER
#define CUSTOMER


typedef int bool;

int r;

typedef struct{

    int arrival;
    int leaving;

} Customer;


static const int MAX_LENGTH = 100;

typedef struct{
    int head;
    int length;

    Customer customer[MAX_LENGTH];

} CustomerLine;

void initializeQueue(CustomerLine* queue)
{
    (*queue).head = 0;
    (*queue).length = 0;
}

bool hasNext(CustomerLine* queue)
{
    return (*queue).length > 0;
}

bool isFull(CustomerLine* queue)
{
    return (*queue).length == MAX_LENGTH;
}

bool enqueue(CustomerLine* queue, Customer* customer)
{


    if(isFull(queue))
        return 0;
    int index = ((*queue).head + (*queue).length) % MAX_LENGTH;
    (*queue).customer[index] = *customer;
    (*queue).length++;

    return 1;
}

Customer* dequeue(CustomerLine* queue)
{
    if(!hasNext(queue))
        return 0;

    Customer* result = &(*queue).customer[(*queue).head];

    (*queue).length--;
    (*queue).head = ((*queue).head + 1) % MAX_LENGTH;
    return result;
}

The error says "Variably Modified 'customer' at file scope" I am a beginner at programming and just doing this is starting to get beyond my abilities so any help would be very much appreciated.

Dan Bechard
  • 4,871
  • 3
  • 32
  • 47
user3050546
  • 11
  • 1
  • 2

2 Answers2

2

The line

static const int MAX_LENGTH = 100

is the problem. Replace it with

#define MAX_LENGTH  100

See why here and more explanations here or here or again here.

Furthermore:

  • You need an #endif after the #ifndef.
  • You need a main function somewhere.
Community
  • 1
  • 1
damienfrancois
  • 45,247
  • 8
  • 85
  • 92
  • Yeah thanks that did solve my problem. also note that i didnt post the full code (its for a class and i dont want to be copied). – user3050546 Nov 29 '13 at 22:13
  • 1
    @user3050546: Welcome to SO! Please read the [About](http://stackoverflow.com/about) page soon. For now, please read [Someone answered my question, what should I do?](http://stackoverflow.com/help/someone-answers). – Jongware Nov 29 '13 at 23:03
1

In C, const means read-only, not constant and usable just like a macro. You cannot use a variable to specify the dimension of an array as you do here:

static const int MAX_LENGTH = 100;
typedef struct{
   int head;
   int length;
   Customer customer[MAX_LENGTH];  /* Wrong. MAX_LENGTH is not a compile time constant. */
} CustomerLine;
Jens
  • 65,924
  • 14
  • 115
  • 171