简体   繁体   中英

Passing struct pointer as a parameter

I'm trying to compile this code in c. First of all i have this struct in a separate source file to use it like a "class" (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);

In second place i have the comms source to retrieve the contents of a directory from a remote file system, and fill the object (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);

Finally, my notebook window interface will look like this. (notebook.h)

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

And its implementation (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;

    ...
}

When i compile this code as is, i get the error saying that

../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
...

since the comms library includes dir.h and notebook.h, and notebook.h does it too. and if i remove the include in notebook.hi got this other error:

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

How can i acheive this? I would like to keep it as clean code as i can.

You included two headers in file comms.c

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

header notebook.h in turn includes header dir.h

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

As result the structure is defined twice in the same compilation unit. You have to provide that each headers would be included only once in each compilation unit. To do this you have to supply header's quards. For example

#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

Or if the compiler supports #pragme once then you could use it.

Usually, multiple declarations are fine in c but multiple definition is not. In your code, you are including dir.h multiple times which is causing the redefinition of struct s_dirobject . Read something about "Header guard" or "Include Guard". here . Hope this solves your major issues with redefinitions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM