简体   繁体   中英

Header file typedef definition

Hi I was triying to make something like this, but I cant sort it out. The problem is one typedef needs the other one. I would really appreciate someones help!

#ifndef SHELL_DATA_H
#define SHELL_DATA_H

#include <buffer.h>

#define COMMANDS 10
#define MAX_ARGS 4

typedef struct {
      void (*command)(int, char **, t_shellData *shelldData);
      char *name;
      char *description;
} t_command;

typedef struct {
      t_command commands[COMMANDS];
      t_buffer buffer;
      char username[BUFFER_SIZE];
} t_shellData;

#endif
typedef struct command t_command;
typedef struct shelldata t_shellData;

struct command {
      void (*command)(int, char **, t_shellData *shelldData);
      char *name;
      char *description;
};

struct shelldata {
      t_command commands[COMMANDS];
      t_buffer buffer;
      char username[BUFFER_SIZE];
};

should fix it up for you. The structure tag and typedef name can be the same; I just renamed them for clarity.

C is a simple language, with an underlying principle of do not surprise people. For this reason, entities in C need to be declared or defined before they are used. As a simple example:

int f() {
     int   a = 7;
     int   b = a;
      ....
}

is OK, but this is not:

int f() {
         int b = a;
         int a = 7;
         ....
}

and while not exactly, languages like golang permit this -- the compiler will root around and find the definition you obviously wanted.

Typedef, in C, really just puts an entry into the symbol table; it is like a define, but less blunt, so the line:

typedef struct a A;

Serves to inform the compiler of two things: somewhere there is a structure with tag a , and I want A to be a shortform for it. There is another form of this:

struct a;
typedef struct a A;

Here, the first line tells the compiler "I want you to know about a thing called struct a"; and the second line "I want an alias to that struct a thing called A".

So, as the compiler progresses through the source, it knows that an A means a struct a , and even if it hasn't seen the definition of struct a , it has a placeholder.

But, if you attempted, before defining struct a to define another structure:

struct b {
       struct a stuff;
       int  morestuff;
};

The compiler would complain, because it doesn't know the layout of a struct a ; however this:

struct b {
    struct a *stuff;
    int morestuff;
};

is OK, because it knows how big a pointer is, and can defer its understanding of a struct a until it needs it.

So, Summary: declare or define data types before you attempt to use them. The C compiler requires it. A declaration is ok, unless you need the actual layout of it, in which case a definition is required.

Good Luck.

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