一元*的类型冲突error和无效类型参数具有int

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

本文关键字:类型参数 无效 int error 类型 冲突 一元      更新时间:2023-10-16

我这里有一个非常简单的程序,我会得到错误,比如:

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') 

我的错误在哪里?

#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;  
} 

问题就在这里:

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

这些线位于函数之外。前两个是可以的,因为它们声明变量,但后两个则不然,因为它们是语句,并且不允许在函数之外使用语句。

出现"冲突类型"错误的原因是,语句(编译器不期望)被读取为声明。由于这个"声明"没有指定类型,所以它有一个隐含的类型int,这与之前类型int *的定义相冲突。

因为您实际想要做的是初始化这些变量(与赋值相反),所以您在声明它们时这样做:

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