Filling struct arrays

Say I declare a struct with one of it's parameters as an array of numbers:

1
2
3
4
struct Year
{
  int *pattern;
};


When I want to fill it with values in main, how do I do that? This is the way I did it but it was wrong:

1
2
Year* leap = new Year;
leap->pattern[12] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
pattern is not an array of numbers, it's a pointer.
Had it been an array, you'd go like this:

1
2
3
4
5
6
7
8
9
struct Year
{
  int pattern[12];
};

int main()
{
    Year leap = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
}
What about the method I used? How would I fill it with the numbers? Or is that not possible because of the pointer?

E:
Your method is still producing errors, or is it just the way I did it?
1
2
3
4
5
6
7
8
9
struct Year
{
  int pattern[12];
  int num;
};

Year* leap = new Year;
leap->pattern = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
leap->num = 366;




In function ‘int main()’:
NumSunday.cc:18:66: warning: extended initializer lists only available with -std=c++0x or -std=gnu++0x [enabled by default]
NumSunday.cc:18:66: error: assigning to an array from an initializer list

Last edited on
It's possible, but not particularly useful.

1
2
3
4
5
6
7
8
9
10
11
12
13
struct Year
{
  int* pattern;
};

int main()
{
    Year leap = {
        new int[12]{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
    };

    delete[] leap.pattern;
}
demo: http://ideone.com/acD3t9

(note: older compilers don't support this syntax, there there's no way to do it in one step)
Topic archived. No new replies allowed.