网宿的一道考题

来源:互联网 发布:vue.js 重置表单 编辑:程序博客网 时间:2024/04/29 11:12
#include <iostream>
#include <cstdlib>
using namespace std;


char *myinet_ntoa(uint32_t in) 

    static char b[18]; 
    register char *p; 
    p = (char *)&in; 
#define UC(b) (((int)b)&0xff) 
    (void) snprintf(b, sizeof(b), 
        "%d.%d.%d.%d", UC(p[0]), UC(p[1]), UC(p[2]),UC(p[3])); 
    return (b); 



int main()
{
 printf("%s, %s\n", myinet_ntoa(0x12345678), myinet_ntoa(87654321)); 
 system("pause");    

}

这道题分析内存的部分都懂,不过败在printf()的执行顺序了,顺序是从右到左的,所以static char b[18]; 的值为左边的结果,基础啊,注意注意!

原创粉丝点击