
- C 標準庫
- C 庫 - 首頁
- C 庫 - <assert.h>
- C 庫 - <complex.h>
- C 庫 - <ctype.h>
- C 庫 - <errno.h>
- C 庫 - <fenv.h>
- C 庫 - <float.h>
- C 庫 - <inttypes.h>
- C 庫 - <iso646.h>
- C 庫 - <limits.h>
- C 庫 - <locale.h>
- C 庫 - <math.h>
- C 庫 - <setjmp.h>
- C 庫 - <signal.h>
- C 庫 - <stdalign.h>
- C 庫 - <stdarg.h>
- C 庫 - <stdbool.h>
- C 庫 - <stddef.h>
- C 庫 - <stdio.h>
- C 庫 - <stdlib.h>
- C 庫 - <string.h>
- C 庫 - <tgmath.h>
- C 庫 - <time.h>
- C 庫 - <wctype.h>
- C 標準庫資源
- C 庫 - 快速指南
- C 庫 - 有用資源
- C 庫 - 討論
- C 程式設計資源
- C 程式設計 - 教程
- C - 有用資源
C 庫 - va_start() 宏
C 的stdarg 庫va_start() 宏允許訪問命名引數parmN之後的可變引數。它用於初始化一個'va_list'變數,然後使用該變數檢索傳遞給函式的其他引數。
在任何對 va_arg(可變引數)的呼叫之前,都應使用有效的 va_list(變數列表)物件ap的例項呼叫va_start。
此宏對於建立可變引數函式很有用,它允許我們建立一個可以接受可變數量引數的函式。它至少包含一個固定引數,後跟省略號(...)。
語法
以下是va_start()宏的 C 庫語法:
void va_start(va_list ap, parmN)
引數
此宏接受以下引數:
-
ap - 它是一個 va_list 型別的變數,將由'va_start'初始化。此變數用於遍歷引數列表。
parmN - 它是函式定義中最後一個命名引數的名稱。
返回值
此宏不返回值。
示例 1
以下是演示va_start()用法的基本 C 示例。
#include <stdio.h> #include <stdarg.h> int sum(int count, ...) { va_list args; int tot = 0; // Set the va_list variable with the last fixed argument va_start(args, count); // Retrieve the arguments and calculate the sum for (int i = 0; i < count; i++) { tot = tot + va_arg(args, int); } // use the va_end to clean va_list variable va_end(args); return tot; } int main() { // Call the sum, with number of arguments printf("Sum of 3, 5, 7, 9: %d\n", sum(4, 3, 5, 7, 9)); printf("Sum of 1, 2, 3, 4, 5: %d\n", sum(5, 1, 2, 3, 4, 5)); return 0; }
輸出
以下是輸出:
Sum of 3, 5, 7, 9: 24 Sum of 1, 2, 3, 4, 5: 15
示例 2
在此示例中,我們使用va_start()來計算使用者定義函式中傳遞的引數數量。
#include <stdio.h> #include <stdarg.h> int cnt(int count, ...) { va_list args; int num_of_arg = 0; // Set the va_list variable with the last fixed argument va_start(args, count); // Retrieve the arguments and count the arguments for (int i = 0; i < count; i++) { num_of_arg= num_of_arg + 1; } // use the va_end to clean va_list variable va_end(args); return num_of_arg; } int main() { // Call the sum, with number of arguments printf("Number of arguments: %d\n", cnt(4, 3, 5, 7, 9)); printf("Number of arguments: %d\n", cnt(5, 1, 2, 3, 4, 5)); return 0; }
輸出
以下是輸出:
Number of arguments: 4 Number of arguments: 5
示例 3
讓我們看另一個例子,在這裡我們建立一個函式,將可變數量的字串連線到單個結果字串中。
#include <stdio.h> #include <stdarg.h> #include <stdlib.h> #include <string.h> char* concatenate(int count, ...) { va_list args; int length = 0; // calculate length va_start(args, count); for (int i = 0; i < count; i++) { length = length + strlen(va_arg(args, char*)); } va_end(args); // Allocate memory for the result string char *res = (char*)malloc(length + 1); if (!res) { return NULL; } // Concatenate the strings // Initialize result as an empty string res[0] = '\0'; va_start(args, count); for (int i = 0; i < count; i++) { strcat(res, va_arg(args, char*)); } va_end(args); return res; } int main() { char *res = concatenate(3, "Hello, ", "tutorialspoint", "India"); if (res) { printf("%s\n", res); //free the alocated memory free(res); } return 0; }
輸出
以下是輸出:
Hello, tutorialspointIndia
廣告