Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4733086
  • 博文数量: 930
  • 博客积分: 12070
  • 博客等级: 上将
  • 技术积分: 11448
  • 用 户 组: 普通用户
  • 注册时间: 2008-08-15 16:57
文章分类

全部博文(930)

文章存档

2011年(60)

2010年(220)

2009年(371)

2008年(279)

分类: LINUX

2009-02-19 11:44:28

  最近一直使用sprintf函数,感慨于内面的不定参数,闲来想研究研究.sprintf(a,)

1:当无法列出传递函数的所有实参的类型和数目时,可用省略号指定参数表
void foo(...);
void foo(parm_list,...);

2:函数参数的传递原理
函数参数是以数据结构:栈的形式存取,从右至左入栈.

3.linux下改写man的一个例子:
 

#include <stdio.h>
#include <stdarg.h>

int foo(char* fmt,...)
{
 va_list ap;
 int d;
 char c, *s;

 va_start(ap, fmt);
 while (*fmt)
  switch(*fmt++)
  {
   case 's':/* string */
    s = va_arg(ap, char *);
    printf("string %s\n", s);
    break;
   case 'd':/* int */
    d = va_arg(ap, int);
    printf("int %d\n", d);
    break;
   case 'c':/* char */
    /* need a cast here since va_arg only
      takes fully promoted types */

    c = (char) va_arg(ap, int);
    printf("char %c\n", c);
    break;
   }
  va_end(ap);
  return 0;
}

int main()
{
  char* a = "aaa";
  int b = 65;
  char c = 'c';

  foo("%s,%d,%c",a,b,c);
  return 0;
}


   其实大家可以看出和sprintf用法差不多....man里面还有个va_copy我这里就不写了,个人觉得用处不大.


4.自己实现个简单的va系列函数.这个程序只支持int输出...别整个char*了说为什么不对.与我而言是不求甚解的.

#include <stdio.h>
#include <stdlib.h>
void myprintf(char* fmt, ...) //一个简单的类似于printf的实现,//参数必须都是int 类型

{
    char* pArg=NULL; //等价于原来的va_list

    char c;
  
    pArg = (char*) &fmt; //注意不要写成p = fmt !!因为这里要对参数取址,而不是取值

    pArg += sizeof(fmt); //等价于原来的va_start

 
    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); //等价于原来的va_arg

        }
        ++fmt;
    }while (*fmt != '\0');
    pArg = NULL; //等价于va_end

    return;
}
int main(int argc, char* argv[])
{
    int i = 1234;
    int j = 5678;
  
    myprintf("the first test:i=%d\n",i,j);
    myprintf("the secend test:i=%d; %x;j=%d;\n",i,0xabcd,j);
    system("pause");
    return 0;
}

好了,这篇over~~~~

阅读(3619) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~