写一个函数判断机器字长 (不用sizeof函数)

#include <stdio.h>


/* using sizeof long int */

int word_size()  

{

    void *p;

    return sizeof(p);     /* the length of pointer */

}


/* using pointer */

int word_size()

{

    void **ptr;

     // 这里如果没用强制类型转换(unsigned long),编译器就会优化这个语句成:ptr+1-ptr,结果就成1了

    return ((unsigned long)(ptr+1)-(unsigned long)(ptr));   /* the length of pointer variable */

}


int main()

{

    printf("The size of machine word is %d\n", word_size());

}


猜你喜欢

转载自blog.csdn.net/somyjun/article/details/23951113