Hello,
I have a confusion about the keyword const.
In the file header data.h I declared a structure
Code:
typedef struct
{
int date_1;
int date_2;
int date_3;
}Date;
In the source file data.c, I declare 100 instances of the Date type:
Code:
Date date_000
Date date_001
Date date_002
.
.
.
Date date_099
Each instance must be passed as a parameter of a function, thus the call is made by reference.
For this I declare an array of pointers of 100 elements:
Code:
Data *dataArray[100]
Each pointer is initialized with the address of an instance:
Code:
Date *dataArray[100] = {&date_000,
&date_001,
&date_002,
.
.
.
&date_099};
The problem is that, if I do such an initialization, the code does not compile, it gives me an error.
Instead, if I initialize each pointer separately, it works.
Code:
Date *dataArray[0] = &date_000;
Date *dataArray[1] = &date_001;
Date *dataArray[2] = &date_002;
.
.
.
Date *dataArray[99] = &date_099;
Looking for information on the Internet, I understood that the initialization must be done in the following way:
Code:
Date *const dataArray[100] = {&date_000,
&data_001,
&data_002,
.
.
.
&data_099};
And, it really works.
I did not understand why the const keyword must be added?
Why array pointers must be declared const array pointers ?
What is the difference between
Code:
Data *dataArray[0] = &data_000
and
Code:
Data *const dataArray[100] = {&data_000, &data_001, &data_002, ... ,&data_099};
?
Thanks.