- 论坛徽章:
- 0
|
本帖最后由 fufelixzh 于 2015-01-08 16:51 编辑
看到网上一段代码,实现print的功能。觉得写的很对,但是运行结果不对,不知道为什么。
如i应该等于1234,结果确实115
#include "stdio.h"
#include "stdlib.h"
void myprintf(char* fmt, ...)
{
char* pArg=NULL;
char *p;
char c;
pArg = (char*) &fmt;
p = fmt;
pArg += sizeof(fmt);
printf("line12:p=%x;pArg=%x\n",p,pArg);
do
{
c =*fmt;
if (c != '%')
{
putchar(c);
}
else
{
switch(*++fmt)
{
case'd':
printf("%d",*((int*)pArg));
break;
case'x':
printf("%#x",*((int*)pArg));
break;
default:
break;
}
pArg += sizeof(int);
}
++fmt;
}while (*fmt != '\0');
pArg = NULL;
return;
}
int main(int argc, char* argv[])
{
int i = 1234;
int j = 5678;
myprintf("the first test:i=%x;%d\n",i,i);
myprintf("the secend test:i=%d; %x;j=%d;\n",i,0xabcd,j);
return 0;
} |
|