当你不知道变量类型的完整定义时可以采取的操作

参考来源:https://stackoverflow.com/questions/9073667/where-to-find-the-complete-definition-of-off-t-type

在Linux下编程时,或者说在一个有很多头文件互相 include 的场景中,经常会遇到不清楚一个变量的完整类型定义的情况(因为有用 typedef 封装),从而有可能遇到编译出错。例如在使用 stat 来读取文件属性的 i-node number 时,查看 stat 的手册,得知这个变量 st_ino 的变量类型是 ino_t,而我们不清楚 ino_t的准确定义究竟是什么。

一般来说可以猜想其是 int 或者 long int,但如果编程时报错,而需要准确得知其变量类型应该怎么办呢?

可以用如下方法:声明一个这样的变量即可。

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

int main() {
  ino_t blah;

  return 0;
}

然后运行如下指令:

gcc -E test.c | grep ino_t

-E 选项的意思是:在预处理过程后结束并输出到标准输出。

-E Stop after the preprocessing stage; do not run the compiler proper. The output is in the form of preprocessed source code, which is sent to the standard output.

输出如下:

可以得知,这个变量是无符号长整型。debug 原来的代码,把占位符从 %d 改成 %ld,也就能编译通过了。

参考来源:https://stackoverflow.com/questions/9073667/where-to-find-the-complete-definition-of-off-t-type

猜你喜欢

转载自www.cnblogs.com/ZCplayground/p/9064210.html