I have a struct which contains some pointers. I want the value of these to be unmodifiable. But simply writing const infront doesn’t make the structs members unmutable
typedef struct{
int *x;
int *y;
}point;
void get(const point *p,int x, int y){
p->x[0]=x;//<- this should not be allowed
p->y[0]=y;//<- this should not be allowed
}
Can someone point me in the right direction.
EDIT:
So it would seem that there is no simple way of using the function prototype to tell that everything belonging to the struct should be unmodifiable
To explain what you need to establish, when you write
Here p is a pointer to str which is of type point
When you declare it as const, it means that p is a constant pointer. This does not restrict the pointers that the structure may contain.
If you want the
constness to apply inside the structure, you have to define the pointers inside the structure also as constAgain to push home my point declare the parameter as
If the structure it is pointing to is also const use