最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C語言可變參數(shù)函數(shù)詳解示例

 更新時間:2013年11月14日 10:05:23   作者:  
一般我們編程的時候,函數(shù)中形式參數(shù)的數(shù)目通常是確定的,在調(diào)用時要依次給出與形式參數(shù)對應(yīng)的實際參數(shù)。但在某些情況下我們希望函數(shù)的參數(shù)個數(shù)可以根據(jù)需要確定,因此c語言引入可變參數(shù)函數(shù)。典型的可變參數(shù)函數(shù)的例子有printf()、scanf()等,下面我就開始講解

先看代碼

復(fù)制代碼 代碼如下:

printf(“hello,world!”);其參數(shù)個數(shù)為1個。
printf(“a=%d,b=%s,c=%c”,a,b,c);其參數(shù)個數(shù)為4個。

如何編寫可變參數(shù)函數(shù)呢?我們首先來看看printf函數(shù)原型是如何定義的。
在linux下,輸入man 3 printf,可以看到prinf函數(shù)原型如下:
復(fù)制代碼 代碼如下:

SYNOPSIS
#include <stdio.h>
int printf(const char *format, ...);

后面的三個點...表示printf參數(shù)個數(shù)是不定的.
如何實現(xiàn)可變參數(shù)函數(shù)?
2. 編寫可變函數(shù)準(zhǔn)備
為了編寫可變參數(shù)函數(shù),我們通常需要用到<stdarg.h>頭文件下定義的以下函數(shù):
復(fù)制代碼 代碼如下:

void va_start(va_list ap, last);
type va_arg(va_list ap, type);
void va_end(va_list ap);
void va_copy(va_list dest, va_list src);

其中:
va_list是用于存放參數(shù)列表的數(shù)據(jù)結(jié)構(gòu)。
va_start函數(shù)根據(jù)初始化last來初始化參數(shù)列表。
va_arg函數(shù)用于從參數(shù)列表中取出一個參數(shù),參數(shù)類型由type指定。
va_copy函數(shù)用于復(fù)制參數(shù)列表。
va_end函數(shù)執(zhí)行清理參數(shù)列表的工作。
上述函數(shù)通常用宏來實現(xiàn),例如標(biāo)準(zhǔn)ANSI形式下,這些宏的定義是:
復(fù)制代碼 代碼如下:

typedef char * va_list; //字符串指針
#define _INTSIZEOF(n) ( (sizeof(n) + sizeof(int) - 1) & ~(sizeof(int) - 1) )
#define va_start(ap,v) ( ap = (va_list)&v + _INTSIZEOF(v) )
#define va_arg(ap,t) ( *(t *)((ap += _INTSIZEOF(t)) - _INTSIZEOF(t)) )
#define va_end(ap) ( ap = (va_list)0 )

使用宏_INTSIZEOF是為了按照整數(shù)字節(jié)對齊指針,因為c調(diào)用協(xié)議下面,參數(shù)入棧都是整數(shù)字節(jié)(指針或者值)。
函數(shù)官方說明,如果你看到英文就煩,可以自行忽略以下說明。
va_start()
       The  va_start() macro initializes ap for subsequent use by va_arg() and
       va_end(), and must be called first.
       The argument last is the name of the last argument before the  variable
       argument list, that is, the last argument of which the calling function
       knows the type.
       Because the address of this argument may  be  used  in  the  va_start()
       macro,  it should not be declared as a register variable, or as a func‐
       tion or an array type.
va_arg()
       The va_arg() macro expands to an expression that has the type and value
       of  the  next  argument in the call.  The argument ap is the va_list ap
       initialized by va_start().  Each call to va_arg() modifies ap  so  that
       the  next  call returns the next argument.  The argument type is a type
       name specified so that the type of a pointer to an object that has  the
       specified type can be obtained simply by adding a * to type.
       The  first use of the va_arg() macro after that of the va_start() macro
       returns the argument after last.   Successive  invocations  return  the
       values of the remaining arguments.
       If  there  is  no  next argument, or if type is not compatible with the
       type of the actual next argument (as promoted according to the  default
       argument promotions), random errors will occur.
       If  ap is passed to a function that uses va_arg(ap,type) then the value
       of ap is undefined after the return of that function.
va_end()
       Each invocation of va_start() must be matched by a corresponding  invo‐
       cation of va_end() in the same function.  After the call va_end(ap) the
       variable ap is undefined.  Multiple traversals of the list, each brack‐
       eted  by va_start() and va_end() are possible.  va_end() may be a macro
       or a function.
GNU給出的一個實例:
復(fù)制代碼 代碼如下:

#include <stdio.h>
#include <stdarg.h>
void
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);
}

說明:
va_start(ap, fmt);用于根據(jù)fmt初始化可變參數(shù)列表。
va_arg(ap, char *);用于從參數(shù)列表中取出一個參數(shù),其中的char *用于指定所取的參數(shù)的類型為字符串。每次調(diào)用va_arg后,參數(shù)列表ap都會被更改,以使得下次調(diào)用時能得到下一個參數(shù)。
va_end(ap);用于對參數(shù)列表進(jìn)行一些清理工作。調(diào)用完va_end后,ap便不再有效。
以上程序給了我們一個實現(xiàn)printf函數(shù)的是思路,即:通過調(diào)用va_start函數(shù),來得到參數(shù)列表,然后我們一個個取出參數(shù)來進(jìn)行輸出即可。
3.實例
例如:對于printf(“a=%d,b=%s,c=%c”,a,b,c)語句;fmt的值為a=%d,b=%s,c=%c,調(diào)用va_start函數(shù)將參數(shù)a,b,c存入了ap中。注意到:fmt中的%為特殊字符,緊跟%后的參數(shù)指明了參數(shù)類型.
因此我們的簡易printf函數(shù)如下:
復(fù)制代碼 代碼如下:

#include <stdio.h>
#include <stdarg.h>
void
myprintf(char *fmt, ...)
{
  va_list ap;
  int d;
  double f;
  char c;
  char *s;
  char flag;
  va_start(ap,fmt);
  while (*fmt){
   flag=*fmt++;
   if(flag!='%'){
 putchar(flag);
 continue;
  }
  flag=*fmt++;//記得后移一位
    switch (flag)
  {
   case 's':
 s=va_arg(ap,char*);
 printf("%s",s);
 break;
   case 'd': /* int */        
 d = va_arg(ap, int);        
 printf("%d", d);        
 break;    
   case 'f': /* double*/        
 d = va_arg(ap,double);        
 printf("%d", d);        
 break;
   case 'c': /* char*/  
 c = (char)va_arg(ap,int);       
 printf("%c", c);       
 break;
   default:
 putchar(flag);
 break;
  }  
  }
  va_end(ap);
}
int main(){
  char str[10]="linuxcode";
  int i=1024;
  double f=3.1415926;
  char c='V';
  myprintf("string is:%s,int is:%d,double is:%f,char is :%c",str,i,f,c);
}

從上面我們可以知道可變參數(shù)函數(shù)的編寫,必須要傳入一個參數(shù)fmt,用來告訴我們的函數(shù)怎樣去確定參數(shù)的個數(shù)。我們的可變參數(shù)函數(shù)是通過自己解析這個參數(shù)來確定函數(shù)參數(shù)個數(shù)的。
比如,我們編寫一個求和函數(shù),其函數(shù)實現(xiàn)如下:
復(fù)制代碼 代碼如下:

int sum(int cnt,...){
    int sum=0;
int i;
    va_list ap;
    va_start(ap,cnt);
for(i=0;i<cnt;++i)
 sum+=va_arg(ap,int);
    va_end(ap);
return sum;
}

總結(jié)一下就是:通過va_start初始化參數(shù)列表(也就能得到具體的參數(shù)個數(shù)了),然后使用va_arg函數(shù)從參數(shù)列表中取出你想要的參數(shù),最后調(diào)用va_end執(zhí)行清理工作。

相關(guān)文章

  • 深入java線程池的使用詳解

    深入java線程池的使用詳解

    本篇文章是對java線程池的使用進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-05-05
  • C語言二維數(shù)組中的查找的實例

    C語言二維數(shù)組中的查找的實例

    這篇文章主要介紹了C語言二維數(shù)組中的查找的實例的相關(guān)資料,需要的朋友可以參考下
    2017-07-07
  • 關(guān)于《C和指針》的學(xué)習(xí)筆記

    關(guān)于《C和指針》的學(xué)習(xí)筆記

    本篇文章是對《C和指針》這本書的學(xué)習(xí)做了筆記介紹。需要的朋友參考下
    2013-05-05
  • C語言實現(xiàn)通訊錄系統(tǒng)

    C語言實現(xiàn)通訊錄系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了C語言實現(xiàn)通訊錄系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-02-02
  • C++ Template 基礎(chǔ)篇(一):函數(shù)模板詳解

    C++ Template 基礎(chǔ)篇(一):函數(shù)模板詳解

    這篇文章主要介紹了C++ Template函數(shù)模板,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • OpenCV 視頻中火焰檢測識別實踐

    OpenCV 視頻中火焰檢測識別實踐

    本文主要介紹了OpenCV 視頻中火焰檢測識別,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • C++設(shè)計模式中控制反轉(zhuǎn)與依賴注入淺析

    C++設(shè)計模式中控制反轉(zhuǎn)與依賴注入淺析

    這篇文章主要介紹了C++設(shè)計模式中控制反轉(zhuǎn)與依賴注入,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧
    2023-01-01
  • opencv平均背景法詳解

    opencv平均背景法詳解

    這篇文章主要為大家詳細(xì)介紹了opencv平均背景法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-03-03
  • C++實現(xiàn)LeetCode(93.復(fù)原IP地址)

    C++實現(xiàn)LeetCode(93.復(fù)原IP地址)

    這篇文章主要介紹了C++實現(xiàn)LeetCode(93.復(fù)原IP地址),本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-07-07
  • C語言實現(xiàn)類似wget的進(jìn)度條效果

    C語言實現(xiàn)類似wget的進(jìn)度條效果

    這篇文章主要介紹了C語言實現(xiàn)類似wget的進(jìn)度條效果的方法,主要是讓大家可以熟練的使用轉(zhuǎn)移符\r,這里推薦給大家,需要的小伙伴參考下。
    2015-03-03

最新評論

克什克腾旗| 茌平县| 绩溪县| 扎鲁特旗| 洱源县| 泽州县| 安国市| 且末县| 四平市| 东源县| 余姚市| 大兴区| 资阳市| 高清| 鄂尔多斯市| 溧水县| 临夏市| 宝清县| 农安县| 错那县| 翁牛特旗| 油尖旺区| 台中县| 靖安县| 灵山县| 镶黄旗| 华容县| 军事| 平阳县| 绥滨县| 恭城| 左权县| 达拉特旗| 舒兰市| 崇州市| 德清县| 新郑市| 大英县| 新化县| 太谷县| 宁化县|