在C89中通过指针传递结构

时间:2010-03-09 18:40:56

标签: c c89

我正在使用C89编译器,我遇到了一些指针输入错误。

致电代码:

struct cpu_state_type cpu_state;
//Stuff here....
foo()
{
    print_out_cpu(&cpu_state);
}

Print_out_cpu在别处定义,H文件在#include中包含。

struct cpu_state_type
{
  int r[12];
};
void print_out_cpu(struct cpu_state_type *c);

我收到错误:

error: incompatible type for argument 1 of 'print_out_cpu'

据我所知,&cpu_state会返回cpu_state_type*类型,所以我很困惑。

2 个答案:

答案 0 :(得分:1)

你确定原型中有*吗?如果我编译(gcc -std=c89)以下代码,我会得到确切的错误:

  struct cpu_state_type {
     int r[12];
  };

  // note that it is the structure as the param here (not the pointer)
  void print_out_cpu(struct cpu_state_type c);
  struct cpu_state_type cpu_state;

  foo()
  {
     print_out_cpu(&cpu_state);
  }

答案 1 :(得分:0)

我没有看到任何问题,所以我想知道你的include语句或文件中是否有错误等。

如果没有看到更多的来源,很难确定错误的原因。尝试创建一个源文件,如:

  

#include struct cpu_state_type cpu_state;

     

void foo(){
      print_out_cpu(安培; cpu_state);   }

如果这不能触发问题,请继续添加内容。 如果它确实触发了问题,请将头文件的相关部分提取到源代码中(并删除#include)并重试。