简体   繁体   English

在C ++中初始化指向结构的指针数组

[英]Initializing an array of pointers to structs in C++

Initializing an array of pointers to structs in C can be done using compound literals. 可以使用复合文字来初始化C语言中的结构的指针数组。

typedef struct {
int a;
int b;
} s;

In C: 在C中:

s *ptrArray[] = {
    &(s){
        .a = 1,
        .b = 2
    },
    &(s){
        .a = 4,
        .b = 5
    }
};

How can this be done in C++? 如何在C ++中完成?

I have also seen the difference in initializing structs in C++ not using compound statements: 我还看到了不使用复合语句在C ++中初始化结构的区别:

s s1 = { a: 7, b: 8 };

First - initializing anything to the address of a temporary value seems extremely fishy, in C as well. 首先-在C语言中,将任何内容初始化为临时值的地址似乎也非常困难。 Are you sure that's valid? 您确定这是有效的吗? Hmmm. 嗯。 Anyway, a C++ compiler will really not let you do that. 无论如何,C ++编译器实际上不允许您这样做。

As for the your designated (named-field) initialization C++ line - it's actually non-standard, it's a GNU C++ extension, and you can't rely on it. 至于您指定的(命名字段)初始化C ++行-它实际上是非标准的,它是GNU C ++扩展,您不能依赖它。

You could do this: 您可以这样做:

struct s { int a, b; };

int main() {
    s data[] = { { 1, 2 }, { 4, 5 } };
    // instead of ptrArray[i], use &(data[i])
}   

This compiles just fine. 这样编译就可以了。 But - a more C++'ish version of this code would be: 但是-此代码的C ++版本将是:

#include <array>

struct s { int a, b; };

int main() {
    std::array<s, 2> data { s{ 1, 2 }, s{ 4, 5 } };
    // instead of ptrArray[i], use &(data[i]),
    // or use iterators, or ranged for loops
}   

Why would you want to use std::array ? 你为什么要使用std::array Here's one explanation of the benefits. 这是好处的一种解释 Actually, you could do slightly better and repeat yourself less with: 实际上,您可以通过以下方法做得更好一些,而重复做的次数更少:

int main() {
    auto data = make_array(s{ 1, 2 }, s{ 4, 5 });
    // instead of ptrArray[i], use &(data[i]),
    // or use iterators, or ranged for loops
}   

The make_array function is taken from here ; make_array函数从这里获取 you also have std::experimental::make_array() , but that's not standardized yet. 您还具有std::experimental::make_array() ,但尚未标准化。

If you want to add or remove elements from data at run-time, you might switch to using std::vector : 如果要在运行时从data中添加或删除元素,则可以切换到使用std::vector

#include <vector>

struct s { int a, b; };

int main() {
    std::vector<s> data { s{ 1, 2 }, s{ 4, 5 } };
    // instead of ptrArray[i], use &(data[i]),
    // or use iterators, or ranged for loops
}   

The reason your initialize was failing is you were attempting to initialize the array of pointers to struct to the address of numeric literal constants. 初始化失败的原因是您试图初始化指针数组以构造为数字文字常量的地址。 The same as: 与:

#define A 5
int b = &A;    /* NOT HAPPENING */

(you can't take the address of 5 ) (您不能使用5的地址)

You can solve your problem by simply initializing an array of s instead of an array of pointers to s , eg: 您可以通过简单地初始化s数组而不是s 指针数组来解决问题,例如:

    s ptrarr[] = { {1, 2}, {4, 5} };

With that change, your array will initialize fine, eg 更改后,您的数组将初始化良好,例如

#include <iostream>

typedef struct {
    int a;
    int b;
} s;

int main (void) {

    s ptrarr[] = { {1, 2}, {4, 5} };
    int cnt = 0;

    for (auto& i : ptrarr)
        std::cout << "ptrarr[" << cnt++ << "] : " << i.a << ", " << i.b << "\n";

}

Example Use/Output 使用/输出示例

$ ./bin/ptrarrystruct
ptrarr[0] : 1, 2
ptrarr[1] : 4, 5

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

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