michaeljt
michaeljt

Reputation: 1176

Is there a good way to force type incompatibility in C?

For purposes of type checking I would like to define a function on the lines of

void myfunc(type1 a, type2 b)
{
...
}

where type1 and type2 are both typedefed to uint8_t. So far so good, but for sanity and checking purposes (think DbC) I would like to prevent the function being called with a type2 value for the first parameter or a type1 for the second. Sadly, C's implicit typecasting is against me here. Does anyone know of a way?

Upvotes: 2

Views: 542

Answers (2)

nuriaion
nuriaion

Reputation: 2631

You could wrap the two types in a Struct.

typedef struct {
    uint8_t data;
} type1;

typedef struct {
    uint8_t data;
} type2;

Edit: I don't like it because you now have to use a.data instead of a

Upvotes: 6

Daniel Băluţă
Daniel Băluţă

Reputation: 1275

I think you can wrap your types using struct and then pass pointer to these structs.

Upvotes: 0

Related Questions