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标准。


  • 相关阅读:
    Dos命令快速设置ip、网关、dns地址
    远程桌面连接保存登陆用户以及密码(凭据)备份方法
    如何启用windows8, windows10中被停用的远程桌面,如何连接windows10远程桌面?
    通过日志恢复SQL Server的历史数据
    http://sourceforge.net/projects/rtspdirectshow/
    iphone上实现H264 硬编码
    利用lipo编译合并iPhone模拟器和真机通用的静态类
    在iOS上使用ffmpeg播放视频
    基于.Net的单点登录(SSO)解决方案
    java实现简单的单点登录
  • 原文地址:https://www.cnblogs.com/aoaoblogs/p/1853313.html
Copyright © 2011-2022 走看看