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

问题描述 投票:0回答:1
struct response {
    string resp[MAXSIZE];
    string type[MAXSIZE];
    int n;
}res;
c++ struct declaration definition typespec
1个回答
2
投票

这是名称为res类型为struct response的对象的声明。

类似,可以将结构定义用作类型说明符

int res;

但是您可以放置​​结构定义而不是类型int

这里是示范节目

#include <iostream>

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

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

    return 0;
}

其输出为

Hello World!

您可以在一行中写对象hello的声明,例如

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

但是这不太可读。

实际上和写相同

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

    Hello hello = { "Hello", "World!" };
    // or 
    // struct Hello hello = { "Hello", "World!" };
© www.soinside.com 2019 - 2024. All rights reserved.