简体   繁体   English

资源的目的是什么; 在以下结构中?

[英]What is purpose of res; in following struct?

struct response {
    string resp[MAXSIZE];
    string type[MAXSIZE];
    int n;
}res;

It is a declaration of an object with the name res of the type struct response .它是一个 object 的声明,名称为res类型为struct response

A structure definition can be used as a type specifier similarly like结构定义可以用作类型说明符,类似于

int res;

but instead of the type int you may place the structure definition但您可以放置结构定义而不是 int 类型

Here is a demonstrative program这是一个演示程序

#include <iostream>

int main() 
{
    struct Hello
    {
        const char *hello;
        const char *world;
    } hello = { "Hello", "World!" };

    std::cout << hello.hello << ' ' << hello.world << '\n';

    return 0;
}

Its output is它的 output 是

Hello World!

You could write the declaration of the object hello in one line like您可以在一行中编写 object hello的声明,例如

struct Hello {  const char *hello; const char *world; } hello = { "Hello", "World!" };

But this is less readable.但这不太可读。

In fact it is the same as if to write其实和写是一样的

    struct Hello
    {
        const char *hello;
        const char *world;
    }; 

    Hello hello = { "Hello", "World!" };
    // or 
    // struct Hello hello = { "Hello", "World!" };

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

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