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 };
2 个解决方案
#1
3
First - initializing anything to the address of a temporary value seems extremely fishy, in C as well. Are you sure that's valid? Hmmm. Anyway, a C++ compiler will really not let you do that.
首先,在C中初始化任何到临时值的地址看起来非常可疑。你确定那是有效的吗?嗯。无论如何,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
? Here's one explanation of the benefits. Actually, you could do slightly better and repeat yourself less with:
为什么要使用std::array?这里有一个关于好处的解释。实际上,你可以做得稍微好一点,少重复自己的话:
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; you also have std::experimental::make_array()
, but that's not standardized yet.
make_array函数取自这里;您还有std::experimental: make_array(),但这还没有标准化。
If you want to add or remove elements from data
at run-time, you might switch to using std::vector
:
如果您想在运行时从数据中添加或删除元素,您可以切换到使用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
}
#2
1
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
, e.g.:
只需初始化一个s数组而不是一个指向s的指针数组,就可以解决问题,例如:
s ptrarr[] = { {1, 2}, {4, 5} };
With that change, your array will initialize fine, e.g.
有了这个更改,您的数组将会很好地初始化,例如。
#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
#1
3
First - initializing anything to the address of a temporary value seems extremely fishy, in C as well. Are you sure that's valid? Hmmm. Anyway, a C++ compiler will really not let you do that.
首先,在C中初始化任何到临时值的地址看起来非常可疑。你确定那是有效的吗?嗯。无论如何,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
? Here's one explanation of the benefits. Actually, you could do slightly better and repeat yourself less with:
为什么要使用std::array?这里有一个关于好处的解释。实际上,你可以做得稍微好一点,少重复自己的话:
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; you also have std::experimental::make_array()
, but that's not standardized yet.
make_array函数取自这里;您还有std::experimental: make_array(),但这还没有标准化。
If you want to add or remove elements from data
at run-time, you might switch to using std::vector
:
如果您想在运行时从数据中添加或删除元素,您可以切换到使用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
}
#2
1
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
, e.g.:
只需初始化一个s数组而不是一个指向s的指针数组,就可以解决问题,例如:
s ptrarr[] = { {1, 2}, {4, 5} };
With that change, your array will initialize fine, e.g.
有了这个更改,您的数组将会很好地初始化,例如。
#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