简体   繁体   English

Header 文件 typedef 定义

[英]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.问题是一个 typedef 需要另一个。 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;结构标签和typedef名称可以相同; I just renamed them for clarity.为了清楚起见,我只是将它们重命名。

C is a simple language, with an underlying principle of do not surprise people. C 是一种简单的语言,其基本原理是不要让人们感到惊讶。 For this reason, entities in C need to be declared or defined before they are used.为此,C 中的实体需要在使用前声明或定义。 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.虽然不完全是,但像 golang 这样的语言允许这样做——编译器将围绕并找到你显然想要的定义。

Typedef, in C, really just puts an entry into the symbol table; typedef,在C中,其实只是在符号表中放了一个入口; 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.用于通知编译器两件事:某处有一个带有标签a的结构,我希望A成为它的缩写。 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";在这里,第一行告诉编译器“我想让你知道一个叫做 struct a 的东西”; and the second line "I want an alias to that struct a thing called A".第二行“我想要该结构的别名,称为 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.因此,随着编译器通过源代码进行,它知道A表示struct a ,即使它没有看到struct a的定义,它也有一个占位符。

But, if you attempted, before defining struct a to define another structure:但是,如果您尝试过,在定义struct a之前定义另一个结构:

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

The compiler would complain, because it doesn't know the layout of a struct 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.没关系,因为它知道指针有多大,并且可以推迟对结构 a的理解,直到它需要它。

So, Summary: declare or define data types before you attempt to use them.因此,总结:在尝试使用数据类型之前声明或定义数据类型。 The C compiler requires it. C 编译器需要它。 A declaration is ok, unless you need the actual layout of it, in which case a definition is required.声明是可以的,除非您需要它的实际布局,在这种情况下需要定义。

Good Luck.祝你好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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