简体   繁体   English

c中的类型声明和声明结构

[英]type declaration and declaring struct in c

I have the following in the header file named vector.h 我在名为vector.h的头文件中具有以下vector.h

typedef struct coordinates coordinates;

Coordinates struct should have two variables. 坐标struct应该有两个变量。 x and y xy

How can I include these two variables x and y without changing anything in header file? 如何在不更改头文件中的任何内容的情况下包括这两个变量xy

my idea was to write the following in main.c 我的想法是在main.c编写以下内容

coordinates{
int x;
int y;
};

I wrote above because I already wrote a typedef struct coordinates in vector.h. 我上面写的是因为我已经在vector.h中写了一个typedef struct coordinates So, if I write again, it is repeated. 因此,如果我再次写,它会重复。 But the above syntax itself is wrong as compiler is throwing error. 但是上述语法本身是错误的,因为编译器会抛出错误。 please help me if I understood structures wrong or help with how to declare variables inside the struct. 如果我理解错误的结构,请帮助我,或者提供有关如何在结构中声明变量的帮助。

This declaration in the header 标头中的此声明

typedef struct coordinates coordinates;

is not very useful because usually a complete structure definition is required in most cases. 并不是很有用,因为通常在大多数情况下都需要完整的结构定义。 So in general it is better to append the header with the complete structure definition. 因此,通常最好将标头附加完整的结构定义。

For example 例如

typedef struct coordinates coordinates;
struct coordinates
{
    int x;
    int y;
};

The single typedef declaration is enough only in cases when there are not required the compete type of the structure. 仅在不需要结构的竞争类型的情况下,单个typedef声明就足够了。 For example when only pointers to objects of the structure are declared. 例如,当仅声明指向结构对象的指针时。

If you may not change the header then include this definition 如果您不可以更改标题,请添加此定义

struct coordinates
{
    int x;
    int y;
};

in a module where the structure is referenced. 在引用结构的模块中。

I have the following in the header file named vector.h 我在名为vector.h的头文件中具有以下内容

 typedef struct coordinates coordinates; 

That's a declaration of the identifier coordinates as an alias for a type struct coordinates , which itself is an "incomplete type" as far as that declaration is concerned. 这是该标识符的声明coordinates为一个类型的别名struct coordinates ,这本身就是一种“不完全型”据该声明而言。

How can I include these two variables x and y without changing anything in header file? 如何在不更改头文件中的任何内容的情况下包括这两个变量x和y?

It is struct coordinates that needs to be "completed" with a definition before you can access members: 在访问成员之前,需要使用定义对struct coordinates进行“完成”:

struct coordinates {
    int x;
    int y;
};

Such a definition needs to be in scope wherever you access the members of an instance of that type, whether by the type name struct coordinates or by its alias coordinates . 无论您通过类型名struct coordinates还是别名coordinates访问该类型的实例的成员,此类定义都必须在范围内。 It is conventional to put such a definition in a header file, so that it is appropriately shared among translation units, but if you need access to the members (or the overall size of the structure) only in one file, then you can instead put the type definition above only in that file. 通常将这样的定义放在头文件中,以便在翻译单元之间适当地共享它,但是如果您只需要在一个文件中访问成员(或结构的整体大小),则可以放一个上面的类型定义仅在该文件中。 Alternatively, you can duplicate the definition identically in every translation unit that wants to access the members, but that's poor form and hard to maintain. 另外,您可以在每个要访问成员的翻译单元中相同地复制定义,但是格式不佳且难以维护。

I'm assuming you mean 我假设你是说

typedef struct coordinates
{
    int x;
    int y;
} coordinates;

then just create a struct with the following line 然后只需用以下行创建一个结构

coordinates c;
c.x = 0;
c.y = 1;

typedef struct coordinates coordinates; typedef结构坐标

is incomplete declaration and hence the compiler will throw an error, when you try to declare the members of struct in main.c file. 是不完整的声明,因此当您尝试在main.c文件中声明struct的成员时,编译器将引发错误。

coordinates{
int x;
int y; 
};

The error thrown will be expected identifier. 引发的错误将是预期的标识符。 The complier is looking for a declaration but it is not able to find it. 编译器正在寻找声明,但找不到它。

You can declare the coordinates struct like this in main.c 您可以在main.c中这样声明坐标结构

typedef struct coordinates{
    int x;
    int y;
}coordinates;

and then you can use it as type. 然后可以将其用作类型。 I would not recommend doing that because it is a repeated exercise and not a good programming practice. 我不建议这样做,因为这是重复的练习,而不是良好的编程习惯。

The best way is to declare the struct in your vector.h file like this: 最好的方法是在vector.h文件中声明结构,如下所示:

typedef struct coordinates{
    int x;
    int y;
}coordinates;

and then use it in your main.c file 然后在main.c文件中使用它

Hope this helps ! 希望这可以帮助 !

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

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