I observed nested structure member get alter despite my const
qualifier declaration, may I know any method for me to avoid nested structure member get alter inside the function?
Code
#include <stdio.h>
#include <stdlib.h>
struct middle_data
{
struct base_data *int_data_ptr;
};
struct base_data
{
int value;
};
void function( const struct middle_data const *middle_ptr)
{
middle_ptr->int_data_ptr->value= 2; // how to forbid value get alter?
struct base_data *ptr= middle_ptr->int_data_ptr;
printf("ptr->value = %d\n", ptr->value);
}
int main(int argc, char **argv)
{
struct middle_data *middle_data_ptr;
middle_data_ptr = (struct middle_data*)malloc(sizeof(*(middle_data_ptr)));
middle_data_ptr->int_data_ptr =malloc(sizeof(*(middle_data_ptr->int_data_ptr)));
middle_data_ptr->int_data_ptr->value=3;
function(middle_data_ptr);
printf(" middle_data_ptr->int_data_ptr->value = %d\n", middle_data_ptr->int_data_ptr->value);
free(middle_data_ptr);
free(middle_data_ptr->int_data_ptr);
return 0;
}
The only way to do this is making the member of the struct base_data
as const