In a C module (aka, compilation unit), I want to have some private data, but expose it read-only to the outside world. I achieve that by having a field in a s
I am not entirely sure if this could be considered a good idea, but something that I do sometimes when I'm feeling lazy is to define a "const cast" macro like:
#define CC(_VAR) ((const typeof(_VAR))(_VAR))
This of course assumes that you have a compiler that supports the typeof
extension. Using this (dubious) construction you could then write
const shape *fooshapes(const foo *f)
{
return CC(f->shapes);
}
Otherwise, C doesn't generally cast to const implicitly. You do have to explicitly cast pointers to const pointers.