-1

I am adjusting existing code and want to avoid renaming variables, etc. I have defined value in one struct[] and want to assign its value to another struct[].

Simplified example. I have:

static const struct {
  int xyz;
} var1[] = { {1}, {2}, {3} };

And what I want to do would look somewhat like this:

static const struct {
  int xyz;
} var2[] = var1;

[...]

if (var2[1].xyz === 1) {
  [...]
}

Basically I need to alias/point to/copy struct[] using another struct[].

Any ideas?

pavel_karoukin
  • 286
  • 3
  • 10
  • You can't assign arrays or uses arrays as initializers in C++. – Barmar Feb 09 '18 at 00:59
  • Initialization lists have to be literal lists. – Barmar Feb 09 '18 at 01:00
  • 1
    Use std::array instead of mundane arrays. – Öö Tiib Feb 09 '18 at 01:00
  • You can make a pointer with: `static const struct { int xyz; } *var2 = var1`; – Barmar Feb 09 '18 at 01:02
  • BTW, why haven't you given a name to the struct, so you don't have to repeat all the members? – Barmar Feb 09 '18 at 01:03
  • If I can not initialize, can I somehow specify variable to be a pointer to existing value? – pavel_karoukin Feb 09 '18 at 01:04
  • I tried this: `static const struct { int xyz; } var1[] = { {1}, {2}, {3} }; static const struct { int xyz; } *var2 = var1;` but got error `error: cannot convert ‘const*’ to ‘const*’ in initialization static const struct { int xyz; } *var2 = var1;` – pavel_karoukin Feb 09 '18 at 01:07
  • With: `static const struct { int xyz; } var1[] = { {1}, {2}, {3} }; static const struct { int xyz; } *var2[] = var1;` I am getting: `error: initializer fails to determine size of ‘var2’ static const struct { int xyz; } *var2[] = var1;` – pavel_karoukin Feb 09 '18 at 01:08
  • What do you mean by *point to* using *struct[]*? – eerorika Feb 09 '18 at 01:10
  • 2
    You are using 2 *anonymous* structs. They have different types. So you can't declare a pointer of one type to point to an instance of the other type. You need to separate your `struct` declaration from your variable declarations, and give the `struct` a type name. Then you can declare variables of the same type. `struct mystruct { int xyz; }; static const mystruct var1[] = { {1}, {2}, {3} }; static const mystruct *var2 = var1;` – Remy Lebeau Feb 09 '18 at 01:11
  • @RemyLebeau, that seems to work! Thank you! – pavel_karoukin Feb 09 '18 at 01:14
  • 1
    @RemyLebeau `auto` or `decltype` can be used to make more instances of the same unnamed struct. (It would be a better idea to give it a name though, as you suggest) – M.M Feb 09 '18 at 01:28
  • OP it is not clear whether you want 2 separate arrays that both have the same content; or whether you just want 2 different names that refer to the same storage. If the latter, then the "duplicate" is not actually a duplicate – M.M Feb 09 '18 at 01:29
  • @M.M, `auto` is actually is perfect with my use-case! I did not know about such feature in C++ Thanks! – pavel_karoukin Feb 09 '18 at 01:37

1 Answers1

2

Your attempt cannot work because 1. Arrays are not assignable, and because 2. your two unnamed classes are separate and unrelated.

can I somehow specify variable to be a pointer to existing value?

You sure can:

static auto var2 = var1;  // pointer to first element
static auto var3 = &var1; // pointer to the array

Prior to C++11, you would have to give a name for the class:

static const struct i_have_a_name {
  int xyz;
} var1[] = { {1}, {2}, {3} };

static const i_have_a_name *var2 = var1;
static const i_have_a_name (*var3)[3] = &var1;
eerorika
  • 232,697
  • 12
  • 197
  • 326
  • 1
    Also possible is `auto& var4 = var1;` which causes the two names `var4` and `var1` have the same meaning – M.M Feb 09 '18 at 01:27