429,373 Members | 1,763 Online
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,373 IT Pros & Developers. It's quick & easy.

# Aggregate Initialization of arrays

 100+ P: 180 Hi guys, Cannot I initialize arrys using aggregate initialization something like this Expand|Select|Wrap|Line Numbers class A {         enum{ sz=10 };       int arr[sz];   public:           A()           {              arr[sz] = {0};           }          ~A(){} }; Please dont tell me to initialize using for loops....neither tell me to use static int arr[sz] and to declare outside like this Expand|Select|Wrap|Line Numbers class A {         enum{ sz=10 };       static int arr[sz];   public:           A()           {              arr[sz] = {0};           }          ~A(){} }; int A::arr[] = {0};  I want to declare arrays at one shot...Isnt that possible Any comments will be highly appreciated Sep 19 '06 #1
6 Replies

 P: 27 Hi I dont have any idea how to initialize array without using loops. I made up tiny loop for that purpose: Expand|Select|Wrap|Line Numbers class A {                     int arr[10];   public:   //my compact constructor:     A()    {      int* i = arr;      while (i<(int*)(&arr+1)) *i++ = 0; //array initialized with zeros    }   //my most compact constructor (only one line of code):     A() {for (int* i=arr; i<(int*)(&arr+1); i++) *i=0;} }   Please post here if you know the more simple way. Thanks Sep 19 '06 #2

 Expert Mod 5K+ P: 8,916 Expand|Select|Wrap|Line Numbers class A {         enum{ sz=10 };       int arr[sz];   public:           A()           {              arr[sz] = {0};           }          ~A(){} };   Wont work because it is not an initialisation it is an assignment, and arr[sz] has type int and is (by definition) not within the bounds of the memory allocated for arr. In fact it is not possible to provide explicit initialisation for array members. You could try using memset though. Why are you so anti loop? I don't think squishing a loop onto 1 line makes it better, it just makes it harder to read. Sep 20 '06 #3

 P: 41 memset why don't you use it? Sep 21 '06 #4

 Expert Mod 5K+ P: 8,916 memset why don't you use it? Strictly speaking it probably still uses a loop internally which is way I wanted to know what the OP had agaist loops. However I agree this is probably the easiest/best method to get the desired result. Sep 21 '06 #5

 P: 27 Hi Yes memset is best method, I didnt know about it :) . However it is usefull only for arrays of chars. What if you want to initialize array of int numbers or long numbers or any other fundamental type to the other value than 0 ? (memset would manage it with zeros of course) I agree that zero initialization is used most often, so most of the time memset is perfect solution, however for non zero value and non char array initialization I still dont know better way than: for int type: for (int* i=arr; i<(int*)(&arr+1); i++) *i=500; float: for (float* i=arr2; i<(float*)(&arr2+1); i++) *i=0.5; ... Sep 21 '06 #6

 P: 41 yes, you're right, DUSH :) Sep 29 '06 #7