2

All I'm trying to do is initialize my array to all 0s in C, but my compiler keeps giving me errors (and the errors aren't helpful). The array has 24 entries and the values are floating point values.

main()
{

/* Array of users arrival & departure time */
float user_queue[24];

/* Initialize queue to 0 */
int i;
for(i = 0; i < 24; i++)
{
    user_queue[i] = 0.0;
}

/* Simulation time */
float time = 0;

The compiler is giving me an error on the "float time" line. The error goes away if I remove my for loop.

syntax error : missing ; before type

0

5 Answers 5

4

You may not be allowed to declare variables after you have already used expressions. Try moving the declaration of time to the top:

main()
{

/* Array of users arrival & departure time */
float time, user_queue[24];

/* Initialize queue to 0 */
int i;
for(i = 0; i < 24; i++)
{
    user_queue[i] = 0.0;
}

/* Simulation time */
time = 0;
Sign up to request clarification or add additional context in comments.

Comments

3

You're overrunning the array by 1 element. Try this instead:

for(i = 0; i < 24; i++)

Change the <= to <.

EDIT : With new information.

You're probably compiling in C89/90 or ANSI C mode. In those older C revisions, variable declarations must be at the start of the function or scope. You can't intermingle declarations and code like that.

Try this:

main()
{

    /* Array of users arrival & departure time */
    float user_queue[24];

    float time;  /* Declare up here */

    /* Initialize queue to 0 */
    int i;
    for(i = 0; i < 24; i++)
    {
        user_queue[i] = 0.0;
    }

    /* Simulation time */
    time = 0;

8 Comments

Oops, yea I just fixed that, but I'm still not able to compile.
Then the error isn't in the code you have provided. You'll need to provide more of the surrounding code.
Ok, thanks for the answer. I had no idea C had that type of restriction.
Yes, but they removed that restriction in C99.
Is there a standard compiler version that is used for C? I'd like some more flexibility if possible, but still use C and not use an obscure version (if such a thing exists). I have Visual Studio 2010, so I assume it has newer compiler versions built into it.
|
3

For this you don't even need a loop:

/* Array of 24 users */
float user_queue[24] = { 0.0 }; 

this will initialize the array to all zeros without the for loop.

1 Comment

FWIW: your answer is referenced here - might want to chime in 'fore folks get stupid.
2

<, not <=, thus:

for( i = 0; i < 24; i++ )

When you create an array like this:

float user_queue[24]

You are creating an array with 24 elements, numbered 0 through 23.

With respect to your updated code, float time = 0; needs to come at the beginning of the main(){.....} block. C's prior to C99 (with the exception of some implementations) didn't let you declare variables except at the beginning of their enclosing scope/block.

Comments

0

Just do this:

float user_queue[24] = {0};

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.