簡體   English   中英

在C89中通過指針傳遞結構

[英]Passing structs by pointer in C89

我正在使用C89編譯器,我遇到了一些指針鍵入錯誤。

來電代碼:

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

Print_out_cpu在別處定義,H文件是#included in。

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* ,所以我很困惑。

你確定原型中有*嗎? 如果我編譯( 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);
  }

我沒有看到任何問題,所以我想知道你的include語句或文件中是否有錯誤等。

如果沒有看到更多的源,就很難確定錯誤的原因。 嘗試創建一個源文件,如:

#include struct cpu_state_type cpu_state;

void foo(){
print_out_cpu(&cpu_state); }

如果這不能觸發問題,請繼續添加內容,直到出現問題為止。 如果它確實觸發了問題,請將頭文件的相關部分提取到源中(並刪除#include)並再試一次。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM