Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I initialize my array?

Tags:

arrays

c

for-loop

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

like image 728
Matt Avatar asked Jan 28 '26 06:01

Matt


1 Answers

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;
like image 94
Robert Martin Avatar answered Jan 30 '26 20:01

Robert Martin