gpt4 book ai didi

c - 将结构指针作为参数传递

转载 作者:太空宇宙 更新时间:2023-11-04 02:01:41 24 4
gpt4 key购买 nike

我正在尝试用 c 语言编译这段代码。首先,我将这个结构放在一个单独的源文件中,以便像“类”(dir.h) 一样使用它

//Structure
typedef struct s_dirobject {
int noteid;
char title[20];
int bytes;
char head[20];
bool is_dir;
struct s_dirobject* next;
} dirobject;

//Ops
void add_dirobject(dirobject* myDirobject,int num_dirobject, char title[20], int is_dir, int bytes, char head[20]);
int get_dirobject_noteid(dirobject* myDirobject,int num_note);
char* get_dirobject_title(dirobject* myDirobject,int num_note);
int get_dirobject_bytes(dirobject* myDirobject,int num_note);
char* get_dirobject_head(dirobject* myDirobject,int num_note);
bool isdir(dirobject* myDirobject, int num_note);
int get_dirobjects_len(dirobject* myDirobject);
void clear_dir(dirobject* myDirobject);
void init_dir(dirobject* myDirobject);

第二,我有通信源来从远程文件系统检索目录的内容,并填充对象 (comms.c)

#include "notebook.h"
#include "dir.h"

dirobject* temporaldirobjects;

...

init_dir(temporaldirobjects);
while(cond) {
//Add retrieved item to the directory
add_dirobject(temporaldirobjects, index, title, is_dir, bytes, "");
}
//When done retrieving the contents from the source i do instantiate the notebook_window
notebook_init(source, path, temporaldirobjects);

最后,我的笔记本窗口界面会是这样的。 (笔记本.h)

#include "dir.h"
void notebook_init(char* source, char* path, dirobject* contents);
void notebook_deinit();

及其实现(notebook.c)

void notebook_init(char* source, char* path, dirobject* contents) {

// Fill the vars
this_window_source=source;
this_window_path=path;
this_window_dirobjects=contents;

...
}

当我按原样编译这段代码时,我得到这样的错误

../src/dir.h:13:16: error: redefinition of 'struct s_dirobject'
In file included from ../src/notebook.h:12:0,
from ../src/comms.c:25:
../src/dir.h:13:16: note: originally defined here
In file included from ../src/comms.c:27:0:
../src/dir.h:20:3: error: conflicting types for 'dirobject'
In file included from ../src/notebook.h:12:0,
from ../src/comms.c:25:
../src/dir.h:20:3: note: previous declaration of 'dirobject' was here
In file included from ../src/comms.c:27:0:
../src/dir.h:23:6: error: conflicting types for 'add_dirobject'
In file included from ../src/notebook.h:12:0,
from ../src/comms.c:25:
../src/dir.h:23:6: note: previous declaration of 'add_dirobject' was here
...

因为 comms 库包括 dir.h 和 notebook.h,而 notebook.h 也有。如果我删除 notebook.h 中的 include,我会收到其他错误:

In file included from ../src/comms.c:25:0:
../src/notebook.h:14:46: error: unknown type name 'dirobject'

我怎样才能做到这一点?我希望尽可能保持代码简洁。

最佳答案

您在文件 comms.c 中包含了两个 header

#include "notebook.h"
#include "dir.h"

header notebook.h 依次包含 header dir.h

#include "dir.h"
void notebook_init(char* source, char* path, dirobject* contents);
void notebook_deinit();

结果,该结构在同一个编译单元中被定义了两次。您必须提供每个 header 在每个编译单元中只包含一次。为此,您必须提供 header 的 quards。例如

#ifndef DIR_H
#define DIR_H

//Structure
typedef struct s_dirobject {
int noteid;
char title[20];
int bytes;
char head[20];
bool is_dir;
struct s_dirobject* next;
} dirobject;

//...

#endif

或者如果编译器支持#pragme once 那么你可以使用它。

关于c - 将结构指针作为参数传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26584758/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com