Prevent assignment of a type in C

137 Views Asked by At

Given a C function:

void f(T x, T y) {
    x = y;
}

I want to make sure that all instances of T assignments will fail. So far, the best solution I have is something like:

#define T const void *

is there a better solution? (Ideally I would like T to be defined as some kind of a non-assignable opaque record pointer type).

2

There are 2 best solutions below

2
On BEST ANSWER

typedef is more suitable here.

For immutable data: typedef const void * T;

For immutable pointer: typedef void * const T;

0
On

I made a dumb mistake. Apparently,

typedef const int T 

or

typedef void * const T

work fine for preventing the assignment.