Chinaunix首页 | 论坛 | 博客
  • 博客访问: 108967
  • 博文数量: 23
  • 博客积分: 1400
  • 博客等级: 上尉
  • 技术积分: 270
  • 用 户 组: 普通用户
  • 注册时间: 2007-12-10 17:58
文章分类
文章存档

2011年(1)

2009年(4)

2008年(18)

我的朋友

分类: C/C++

2009-02-26 15:02:04

下面的代码 屏蔽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) |
给主人留下些什么吧!~~