zoukankan      html  css  js  c++  java
  • [C语言]关于指针和int型的一道题目

    题目就下面几行,问printf的输出是什么?

    int main()
    {
        unsigned int a[3] = {0x010203, 0x040506, 0x070809};
        unsigned int *b0 = (unsigned int*)( (int)a + 1);
        printf("%x\n", *b0);
        return 0;
    }
    

    在某个论坛上看到这个问题,展开讨论以后,其涉及到了不少的C语言知识。各位先不妨自己想想你自己的答案是什么。

    --------------------------------------------------------分割线-------------------------------------------

    在VS2008下,输出是6000102。原因如下:
    a[3]数组在内存中的存储方式如下
    低地址-------------------------------->高地址
    03 02 01 00 06 05 04 00 09 08 07 00
         ^^^^^^^^
         b0指向这里
    所以printf打印出来的值是6000102
    也许大部分的面试,问到这里估计就结束了,可以得满分了。
    不过在我看来,顶多能得30分。

     很明显这个题的是一个编译器实现相关的问题。还应该考虑些什么呢?

    1.大小端的问题。在大端机器上,可能的答案是1020300

    2.32bits或64bits地址的问题,如果是64bits地址和32bits整形(int)的话,很明显(int)a这句话就会有问题了

    3.int的大小一定是32bits吗?当然不是了,C99标准中如下说
       5.2.4.2.1Sizes of integer types <limits.h>

        ...

           Their implementation-defined values shall be equal or greater in magnitude (absolutevalue) to those shown, with the same sign.

        ...

       —maximum value for an object of type unsigned int

       UINT_MAX65535 // 2^16 - 1

    4. unsigned int *b0 = (unsigned int*)((int)a + 1);这句话真的是安全的吗?

         必然不是的,整个这个语句就是一个implementation-defined的,C99标准如下说

         An integer may be converted to any pointer type. Except as previously specified, the result is implementation-defined, might not be correctly aligned, might not point to an entity of the referenced type, and might be atrap representation.

         Any pointer type may be converted to an integer type. Except as previously specified, the result is implementation-defined. If the result cannot be represented in the integer type, the behavior is undefined. The result need not be in the range of values of any integer type.

     

    假设我是面试官的话,能答出大小端和64bits,就是蛮不错的了

    至于答出第3,4点就比较困难了,会阅读C99标准的应该不多。

    标准中是否还有其他和这个题相关,我也不知道,我也没有通读C99标准。


  • 相关阅读:
    gvim在windows下的一些小技巧
    解决eclipse在ubuntu下无法找到jdk方法
    ubuntu 12.04 下安装wireshark
    使用坚果云同步数据
    Windows WMIC命令详解 (Windows Management Instrumentation Commandline)
    ubuntu 12.04 配置指南
    CHROME自定义样式扩展 —— STYLISH
    地漏
    卫生间装修,想要坐便改成蹲便,地面需要加高多少?
    不锈钢橱柜
  • 原文地址:https://www.cnblogs.com/aoaoblogs/p/1853313.html
Copyright © 2011-2022 走看看