下面的代码 屏蔽putchar和不屏蔽putchar 打印的结果是不同的,putchar会对引用的字符串有影响??
[code]
#include
#include
#include
#include
typedef struct {
int Id;
int DevId;
char Name[32];
char Desc[64];
}DevStateDesc;
DevStateDesc data[4] = { {1,11,"name11","desc11"},
{2,22,"name22","desc22"},
{2,22,"name222","desc222"},
{3,33,"name33","desc33"} };
#ifndef WIN32
DevStateDesc *getData(char* format,...)
#else
DevStateDesc *getData(char* format,va_alist)
va_dcl
#endif
{
va_list list;
char *tmp = NULL;
int arg_len = 0;
int i=0;
char *string = strdup(format);
char *sep = "%";
char *token = NULL;
#ifndef WIN32
va_start(list,format);
#else
va_start(list);
#endif
//test the first wheather is NULL
if (va_arg(list,char*) == NULL )
{
goto END_CODE;
}
else
{
va_end(list);
//start again
#ifndef WIN32
va_start(list,format);
#else
va_start(list);
#endif
}
//token by %
token = strtok(string,sep);
while( token != NULL)
{
//printf("%s\n",token);
char c = *token;
//putchar(c);
{
switch(*token)
{
case 'd':
printf("%s is int type %d\n",++token,va_arg(list,int));
break;
case 's':
printf("%s is char pointer %s\n",++token,va_arg(list,char*));
break;
default :
printf("default");
break;
}
}
token = strtok(NULL,sep);
}
END_CODE:
va_end(list);
free(string);
return NULL;
}
int main(int argc, char *argv[])
{
getData("%dId%sName",2,"name22",NULL);
return 1;
}
[/code]
屏蔽的打印结果是
Id is int type 2
Name is char pointer name22
不屏蔽的打印结果是
dId is int type 2
sName is char pointer name22
阅读(1412) | 评论(0) | 转发(0) |