I have a structure (this is kinda irrelevant for my question), and in this structure, a DATE type is used.
typedef div_t DATE;
#define ans quot /* (s)(l)div_t : .quot -> .ans... */
#define mois rem /* ... et .rem -> .mois : plus parlant */
DATE is of type div_t containing a year as quot and a month (mois) as rem. (... :) )
Anyhow.
I would like to make an array of elements for this DATA (div_t) type.
This seems to work as follows:
DATE tx_tech_variable_dates_vec[8] = {0, 0}; /* code testing */
However, I don't know how to assign elements (year/quot, mois/rem) to this array:
This does not work:
DATE tx_tech_variable_dates_vec[0].quot = { 2026, 06 }; /* code testing */
DATE tx_tech_variable_dates_vec[0].rem = { 2026, 06 }; /* code testing */
//DATE tx_tech_variable_dates_vec[1].ans = { 2026, 07 }; /* code testing */
//DATE tx_tech_variable_dates_vec[1].mois = { 2026, 07 }; /* code testing */
//DATE tx_tech_variable_dates_vec[1] = { 2032, 06 }; /* code testing */
Any idea how to create an array of "DATE"(s)/div_t elements and assign elements (quot, rem) to it?
I tried also this:
In the same .cpp file, I have these lines of code:
double tx_tech_variable_vec[8] = { 0 };
tx_tech_variable_vec[0] = 3.0;
tx_tech_variable_vec[1] = 1.5;
DATE tx_tech_variable_dates_vec[8] = { 0, 0 };
DATE tx_tech_variable_dates_vec[1] = { 2026, 07 };
with this error:
Line Severity Code Description Project File Suppression State 4680 Error C2369 'tx_tech_variable_dates_vec': redefinition; different subscripts
For tx_tech_variable_vec
it seems to work fine, but not for DATE.
To reply to some comments:
Yes I know this is not the best way of doing it, but yes there are 90000 lines of code in the program I work with, that already use this DATE structure and this array way of working with things. So just to add a few lines of code, I will not introduce vectors or get rid of the macros or invent another DATE type that is not compatible with the rest of the (indeed very questionable) code.
Assigning to an array element is no different from assigning to a non-array variable. For instance tx_tech_variable_dates_vec[0] = { 2026, 06 };
would assign to the value { 2026, 06 }
to the first element of tx_tech_variable_dates_vec
.
Note that this is assignment though. You're modifying the existing array element to match the value given. It will have already been either zero-initialized or default-initialzed when the array was created (depending on how you initialized the array).
If you want to initialize the elements in tx_tech_variable_dates_vec
when the array is created, then you can include initializers for the elements you want to initialize in the array's initializer.
For instance, the following will initialize the first three elements of tx_tech_variable_dates_vec
with the given values:
DATE tx_tech_variable_dates_vec[8] = {
{ 2026, 06 }, { 2026, 12 }, { 2027, 03 }
};
Note that any elements you don't provide an initializer for will be zero-initialized.