#include <iostream>
#include <cstdlib>
using namespace std;
char *myinet_ntoa(uint32_t in)
{
static char b[18];
register char *p;
p = (char *)∈
#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");
#include <cstdlib>
using namespace std;
char *myinet_ntoa(uint32_t in)
{
static char b[18];
register char *p;
p = (char *)∈
#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]; 的值为左边的结果,基础啊,注意注意!

本文深入探讨了使用C++语言解析IPv4地址并进行格式化输出的过程,通过静态变量和字符串格式化函数实现。重点强调了printf()函数执行顺序对输出结果的影响,以及内存管理在编程实践中的重要性。
1719

被折叠的 条评论
为什么被折叠?



