简体   繁体   English

用结构前缀初始化是什么意思?

[英]What does initialisation with struct prefix mean?

I was reading some code and came across the following statement:我正在阅读一些代码并遇到以下语句:

 struct sockaddr_in server

I understand that sockaddr_in is some predefined struct but why do we put struct as a prefix?我知道sockaddr_in是一些预定义的struct但为什么我们把struct作为前缀? Also shown below, a similar thing is attempted也如下所示,尝试了类似的事情

在此处输入图片说明

Cannot I just write:我不能只写:

sockaddr_in server

The syntax of the C language requires that a declaration of a variable of a struct type include the struct keyword before the type. C 语言的语法要求struct类型变量的声明在类型之前包含struct关键字。

While C++ does allow this keyword to be omitted, C does not.而C ++确实允许将被省略的该关键字,C则没有。

In C there is such a notion as a tag of a structure, union or enumeration.在 C 中有这样一个概念,如结构、联合或枚举的标记。 Correspondingly an identifier can denote a tag.相应地,标识符可以表示标签。 To distinguish such an identifier from an identifier of other variable identifiers that denote tags are used together with keywords struct, union, and enum.为了将此类标识符与其他变量标识符的标识符区分开来,表示标记的标识符与关键字 struct、union 和 enum 一起使用。

For example consider the following demonstrative program.例如,考虑以下演示程序。

#include <stdio.h>

int main(void) 
{
    struct x
    {
        int x;
    } x = { 10 };

    printf( "x.x = %d\n", x.x );

    return 0;
}

Its output is它的输出是

x.x = 10

Here the same name x is used to denote a structure tag, a member of a structure and a variable of a structure type.这里同名x用于表示结构变量、结构成员和结构类型变量。

In C++ there is used the term class name the same way as a name of any other entity.在 C++ 中,术语类名的使用方式与任何其他实体的名称相同。 In C++ there are name spaces that allows to place entities with the same name in different name space to avoid a collision.在 C++ 中有一些命名空间,允许将具有相同名称的实体放在不同的命名空间中以避免冲突。

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

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