My code passes around pointers to scalar types that represent different things that are easily confused. I thought the compiler could help me with this.
Here is a test program:
typedef int type_a;
typedef int type_b;
type_a do_something_with_a(type_a* pa)
{
return *pa + 3;
}
int main(void)
{
type_b b = 0;
do_something_with_a(&b); /* This is a bug. */
return 0;
}
I am passing a B to a function that is only supposed to do things with As, and yet gcc does not even throw a warning:
$ gcc -Wall typesafe2.c
$
Is there an elegant way I can write equivalent code but have the compiler throw errors when I make such a terrible mistake?