C“__builtin_types_compatible_p”函数返回“char *”类型的错误值

时间:2016-04-08 18:01:59

标签: c types built-in

__builtin_types_compatible_p是一个应该比较两种类型的函数,如果它们相等则返回true,如果它们不是同一类型则返回false。出于某种原因,当我处理char*类型时,这似乎不起作用。

示例:

printf("%d\n", __builtin_types_compatible_p(typeof(1), int));
printf("%d\n", __builtin_types_compatible_p(typeof("thing"), char *));

这两行将产生输出:

1
0

这表明"thing"不属于char*类型,这是不正确的......我怎样才能让它正常工作?

1 个答案:

答案 0 :(得分:3)

这是正确的;字符串文字的类型为:char[] char*char[] 是不同的类型

检查:

#include <stdio.h>

int main(void) {
        printf("%d\n", __builtin_types_compatible_p(typeof(1), int));
        printf("%d\n", __builtin_types_compatible_p(typeof("thing"), char[]));
        return 0;
}

输出:

1
1

附录:

  1. What is the type of string literals in C and C++?
  2. Char array vs Char Pointer in C
  3. What is the difference between char s[] and char *s?
  4. Difference between char* and char[] in C