I ran into the following in some legacy C code:
typedef struct _somestruct_ {
/* .... */
} SomeStruct_t
static void do_one_thing(SomeStruct_t *pInput){
/* Do some read-only stuff with pInput */
}
static void do_many_thing(SomeStruct_t input){
do_one_thing(&input);
}
Does C actually permit this, even though the thing will likely blow up if do_one_thing is actually modified to write to pInput?
This is completely legal C code. The code
do_one_thing(&input)is simply passing the address of a parameter to the functiondo_one_thing. There is nothing wrong with this.It’s not even an issue if
do_one_thingwrites topInput. The address is valid and points to a mutable value. There is nothing wrong with doing an update. Consider