[英]How to understand this kind of function declaration?
誰能建議我下面的代碼行代表什么?
static int(*pfcn[2]) (char *, ...) = { (void *)printf, (void *)NULL };
g↔英語是一個不錯的網站,可幫助您解釋聲明
// declare pfcn as array 2 of pointer to function (pointer to char, ...) returning int
int(*pfcn[2]) (char *, ...)
{ (void *)printf, (void *)NULL };
使用函數printf()
初始化該數組,然后使用NULL
初始化,這可能表示結束。
int printf(const char *format, ...)
NULL
static
表示數組是本地數組,並且只能由其所在的function / C文件訪問。
@Lundin建議編譯得很好。
// { printf, (void *) NULL };
{ printf, NULL };
海事組織,宣言也應該是
// const added
static int(*pfcn[2]) (const char *, ...) = { printf, NULL };
注意:某些C可能不允許將NULL
強制轉換為函數指針。 在這種情況下,代碼可以使用
static int printf_null(const char *format, ...) {
return 0;
}
static int(*pfcn[2]) (const char *, ...) = { printf, printf_null };
...並針對printf_null
而不是NULL
進行測試以檢測結束。 避免強制轉換是一件好事。
pfcn
是一個函數指針數組。
這些函數是在返回int
時采用可變數量的args的函數。
它是兩個函數數組的(很難讀)定義。 我會這樣寫:
#include <stdio.h>
#include <stdlib.h>
typedef int (*Function)(const char *format, ...);
static Function pfcn[2] = {printf, NULL};
點表示函數將在第一個參數之后接受零個或多個參數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.