簡體   English   中英

C在頭文件中轉發struct

[英]C forward declaration of struct in header

我試圖在函數中傳遞struct指針。 我在file1.h中有一個typedef ,並希望只將該頭包含到file2.c中,因為file2.h只需要指針。 在C ++中我會像我在這里寫的一樣,但是使用C99它不起作用。 如果有人有任何建議如何傳遞struct指針沒有完整的定義,將非常感激。 編譯器 - gcc。

file1.h

typedef struct
{
    ...
} NEW_STRUCT;

file2.h

struct NEW_STRUCT;

void foo(NEW_STRUCT *new_struct); //error: unknown type name 'NEW_STRUCT'

file2.c中

#include "file2.h"

#include "file1.h"

void foo(NEW_STRUCT *new_struct)
{
    ...
}

我想你只需要命名你的結構,並做一個前向聲明,然后重新輸入它。

第一檔:

 typedef struct structName {} t_structName;

第二檔:

  struct stuctName;
  typedef struct structName t_structName

你可以試試這個:

file1.h

typedef struct _NEW_STRUCT  // changed!
{
    ...
} NEW_STRUCT;

file2.h

struct _NEW_STRUCT; // changed!

void foo(struct _NEW_STRUCT *new_struct); // changed!

file2.c中

#include "file2.h"
#include "file1.h"

void foo(NEW_STRUCT *new_struct)
{
    ...
}

暫無
暫無

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

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