简体   繁体   English

如何使用另一个文件中的结构?

[英]How to use a struct from another file?

I am trying to use an *.h named structures in other files like clube.c which will create an array from the struct defined as Clubes. 我正在尝试在其他文件(如clube.c)中使用* .h命名结构,该文件将根据定义为Clubes的结构创建一个数组。

structures.h: structures.h:

extern typedef struct Clubes{
 char *codClube;
 char *nome;
 char *estadio;
};

no matter what I do it just doesn't appear in my clubes.c 无论我做什么,都不会出现在我的clubes.c中

Note: already did the include "structures.h" 注意:已经包含了“ structures.h”

Once again thanks in advance, and if theres any info that I can give to help you help me just ask. 再次感谢您,如果有什么我可以帮助您的信息,请问我。

Keyword typedef just lets you define type, for example, typedef int whole_number this will create type whole_number and now you can use it as you would use int , whole_number x = 5; 关键字typedef只是让您定义类型,例如, typedef int whole_number会创建whole_number类型,现在您可以像使用int一样使用它, whole_number x = 5; Same thing with structures. 与结构相同。 People use typedef on structures so they don't need to write struct Clubes : 人们在结构上使用typedef,因此他们不需要编写struct Clubes

typedef struct Clubes{
 char *codClube;
 char *nome;
 char *estadio;
} clubes;

And now you don't have to use struct Clubes x; 现在,您不必使用struct Clubes x; , you can use clubes x; ,您可以使用clubes x; which is shorter and easier to write. 它更短,更容易编写。

Extern keyword is giving you global linkage, and in this case it doesn't do anything. Extern关键字为您提供了全局链接,在这种情况下,它没有任何作用。

Your question is a little bit confusing. 您的问题有点令人困惑。 If you want to create this structure, and then use it in other files you need to create header file: 如果要创建此结构,然后在其他文件中使用它,则需要创建头文件:

#ifndef __CLUBES_H_
#define __CLUBES_H_ 1

struct Clubes{
 char *codClube;
 char *nome;
 char *estadio;
} clubes;

#endif

Save this in one header file, for example clubes.h and then in some other c code where you want to use this structure you just include header with #include "clubes.h" 将其保存在一个头文件中,例如clubes.h ,然后在其他要使用此结构的c代码中,只需在#include "clubes.h"包含头文件#include "clubes.h"

Perfect use for header files 完美使用头文件

/* put this in clube.h */
struct Clubes{
 char *codClube;
 char *nome;
 char *estadio;
};

/* put this in clube.c */
#include "clube.h"

struct Clubes myarray[5];

I think you're confusing what some of these keywords do. 我认为您对这些关键字的某些功能感到困惑。

struct creates a new type. struct创建一个新类型。 This doesn't create any instances of a type. 这不会创建任何类型的实例。

extern is for providing linkage for a global variable. extern用于为全局变量提供链接。

typedef is giving a new global name to an existing type. typedef为现有类型赋予新的全局名称。

/* clube.h */

/* define and declare the Clubes struct */
struct Clubes {
    char* codClube;
    char* nome;
    char* estadio;
}

/* typedef the Clubes struct.
 * Now you can say struct Clubes or Clubes_t in your code.
 */
typedef struct Clubes Clubes_t;

/* Now you've created global linkage for my_clubes.
 * I.e. two cpp files could both modify it
 */
extern struct Clubes my_clubes[2];



/* clube.c */

#include "clube.h"

/* Now you've got storage for my_clubes */
Clubes_t my_clubes[2];


/* some_other.c */

#include "clube.h"

void fun() {
    my_clubes[0].codClube = "foo";
    /* etc... */
}

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

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