struct response {
string resp[MAXSIZE];
string type[MAXSIZE];
int n;
}res;
这是名称为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!" };