Easy Syntax Question

Variable theNumbers is a pointer to an array of ints in myStruct.

This is OK.

1
2
int numbers[] = {1, 1, 1};
myStruct.theNumbers = numbers;


How can I do it in one line? This is how I would have LIKED it to work.

 
myStruct.theNumbers = {1, 1, 1};

closed account (o3hC5Di1)
Hi there,

int numbers[] = {1, 1, 1}; myStruct.theNumbers = numbers;

All jokes side, I don't think that's possible (without some expert magic).
For as far as I know, you can only use the curly brace format when initialising an array, not when assigning, and definitely not when assigning pointers.

I may be wrong though, or possibly one of the experts here may have a solution for you.

All the best,
NwN
Hohoho :)

Experts?
Use std::vector<> ?
http://www.codeguru.com/cpp/cpp/cpp_mfc/stl/article.php/c4027/C-Tutorial-A-Beginners-Guide-to-stdvector-Part-1.htm

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <vector>

struct A
{
    // ...
    std::vector<int> numbers ;
};

int main( )
{
    A a ;
    a.numbers = { 1, 2, 3 } ;
}

Topic archived. No new replies allowed.