C Question

Conflicting types error and invalid type argument of unary * have int

I have very simple program here, I get errors like :

conflicting type of 'f'

previous declaration of 'f' was here

error : initializer element is not computable at load time

in function queue_ready:

invalid type argument of unary '*' (have'int')

in function dequeue_ready :

invalid type argument of unary '*' (have'int')


Where are my mistakes?

#include<stdio.h>

int Queue_ready[1000];
int *r ;
int *f ;
r=&Queue_ready[0];
f=&Queue_ready[0];

void queue_ready (int process)
{
*r=process;
r = r+1;
}

void dequeue_ready (void)
{
*f = 10000;
f=f+1;
}

int main()
{
queue_ready(1);
queue_ready(2);
printf("%d %d" , Queue_ready[0] ,Queue_ready[1]);
dequeue_ready();
dequeue_ready();
return 0;
}

Answer

The problem is here:

int *r;
int *f; 
r=&Queue_ready[0];
f=&Queue_ready[0];

These lines lie outside of a function. The first two are OK, since they declare variables, but the next two are not since they are statements, and statements are not allowed outside of a function.

The reason you're getting a "conflicting type" error is because the statement (that the compiler isn't expecting) is being read as a declaration. Since this "declaration" doesn't specify a type, it has an implied type of int which conflicts with the prior definition of type int *.

Because what you actually want to do is initialize these variables (as opposed to assign), you do so at the time they are declared:

int *r = &Queue_ready[0];
int *f = &Queue_ready[0];
Comments