I was studying the Read-Only compound literals in C. And when I tried to change its value with the help of dereference operator, the value got changed!! I am now confused why is that so.
Also when I compiled and run the program (Without attempting to change its value) it shows this error:
Code_Testing.c:5:14: warning: initialization discards 'const' qualifier from pointer target type [-Wdiscarded-qualifiers]
5 | int *r = (const int[3]){6, 14, -98}; // Read Only compound Literal
|
I cannot understand why it is ignoring the const
qualifier.
I know that const
qualifier discards any change, but here the value of the compound literal changed!
Can you please explain me where I had committed a mistake?
The program I wrote is this:
#include <stdio.h>
int main(void)
{
int *r = (const int[3]){6, 14, -98}; // Read Only compound Literal
*r += 99;
printf("The changed value is = %d", *r);
return 0;
}
The output is:
Code_Testing.c:5:14: warning: initialization discards 'const' qualifier from pointer target type [-Wdiscarded-qualifiers]
5 | int *r = (const int[3]){6, 14, -98}; // Read Only compound Literal
| ^
The changed value is = 105