码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2018-05-20 19:22:50      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:not run   运行   怎么   question   block   属性   orm   很多   无符号   

参考来源: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

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

标签:not run   运行   怎么   question   block   属性   orm   很多   无符号   

原文地址:https://www.cnblogs.com/ZCplayground/p/9064210.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!