News for hackers. Stuff that matters.
Re:My personal C coding style as of late 2023 by david2ndaccount on Sunday October 08, 2023 @10:49PM (#37816446)
You only know there are no mutations if Foo itself does not contain any indirections. Additionally, the compiler generally cannot assume that Foo_bar does not modify Foo as it is legal to cast away const as long as it is not originally a variable declared as const (so in your static Foo example it would be UB to cast away const).

static + const is valuable, but const parameters are merely a convention, there is no actual enforcement around them and due to aliasing the compiler generally can’t assume the parameter doesn’t actually change anyway.

Re:My personal C coding style as of late 2023 by lelanthran on Monday October 09, 2023 @03:17AM (#37817861)
> Additionally, the compiler generally cannot assume that Foo_bar does not modify Foo as it is legal to cast away const

No, but it can warn you!

The type is meant to capture programmer intention, and if you use `const` the compiler can warn you that your intention does not match the intention of the existing code (like, the intention of the author who wrote Foo_Bar).

Re:My personal C coding style as of late 2023 by jpcfl on Monday October 09, 2023 @10:30AM (#37820813)
True. I should rephrase to say `const` strongly suggests that a function does not change the observable state of a variable.