Hi everyone!
I have some doubts about const arrays and pointers in C.
Given the following code
Code:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv)
{
const char array[] = "this is an array";
// array[0] = 'j';//error: assignment of read-only location ‘array[0]’
char *p = (char*)array;//warning: initialization discards ‘const’ qualifier from pointer target type
p[0] ='j';
printf("array = %s\n",array);
return (EXIT_SUCCESS);
}
If 'array' is declared as constant I can not change the array. But why can I use a pointer to the array 'p' and alter it the original array 'array'?
Another question is that if I cast the assignation to the array
Code:
char *p = (char*)array;
The warning disappears. Why?
Thank you all very much!