#include <stdio.h>
int main()
{
const int marbles[10] = { 1,2,3,4,5,6,7,8,9,10 };
int *ptr = marbles;
*ptr = 100;
printf("%d \n", marbles[0]); // output is "100"
}
I used const
to protect the array. So I thought int *ptr = marbles
would cause error. Otherwise by using pointer, it would enable user to change the data in the array. But surprisingly the output is "100". Isn't C suppose to protect the array from any kind of methods when I use const
?