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?


int Queue_ready[1000];
int *r ;
int *f ;

void queue_ready (int process)
r = r+1;

void dequeue_ready (void)
*f = 10000;

int main()
printf("%d %d" , Queue_ready[0] ,Queue_ready[1]);
return 0;

Answer Source

The problem is here:

int *r;
int *f; 

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];