3

How do I write a macro CHECK(a, b) that generates compilation error when the two pointers a & b have different base type.

CHECK((int*)0, (char*)0) -> compilation error
CHECK((int*)0, (int*)0) -> works

I'm looking for some C89 code, but C99 + gcc extensions will also do.

Alexandru
  • 25,070
  • 18
  • 69
  • 78

3 Answers3

4

EDIT now works for any type, not just pointers

Something more or less lifted from the linux kernel, using the GCC extension typeof().

This generates a warning at compile time, it also works for integer pointer types

#define CHECK(a, b) do { \
    typeof(a) _a; \
    typeof(b) _b; \
    (void) (&_a == &_b); \
    } while (0)

int main(int argc, char **argv)
{
    int *foo;
    int *bar;
    char *baz;

    CHECK(foo, bar);
    CHECK(bar, baz);
    return 0;
}
Hasturkun
  • 35,395
  • 6
  • 71
  • 104
  • It won't work for pointers to integer types - compiler will promote smaller type to bigger one and no error will occur – qrdl Aug 18 '09 at 12:57
  • have you tried it? for this program gcc outputs the following: `a.c:16: warning: comparison of distinct pointer types lacks a cast` I always compile with the `-Werror` flag, btw. – Hasturkun Aug 18 '09 at 14:03
  • Anyway, pointer types do not get promoted, that would make very little sense. their dereferenced contents might, but they aren't being examined here. – Hasturkun Aug 18 '09 at 14:07
0

I don't know how to do it using macros. If you can use C++, then take a look here

Naveen
  • 74,600
  • 47
  • 176
  • 233
0

You can try this way:

#define CHECK(A,B) do { typeof(*A) _A; typeof(*B) _B; _A = _B; } while (0)

If your base types and not integer types (char, int, short, long, long long), types will not be promoted so assignment _A = _B will fail.

I don't think there is a way to make it working for integer types.

qrdl
  • 34,062
  • 14
  • 56
  • 86