[英]typedef - Typedef without curly braces
我是 C 的新手,但是這個語法是什么意思?
typedef Value (*NativeFn)(int argCount, Value* args);
據我了解,這里使用“值”來定義新名稱的類型。 我不明白的部分是(*NativeFn)(int argCount, Value* args);
,這部分是什么意思?
其他人正確地說:
typedef Value (*NativeFn)(int argCount, Value* args);
為指向函數的類型創建 typedef 名稱NativeFn
。
typedef
的語法,就像一般的 C 聲明的語法一樣,可能會令人困惑。 typedef
特性實際上是在聲明語法建立之后添加到語言中的,並且必須在不破壞任何其他內容的情況下添加它。 解決方案是在語法上將typedef
視為存儲 class 說明符(盡管它在語義上不是一個)。 除了typedef
之外的存儲 class 說明符是extern
、 static
、 _Thread_local
、 auto
和register
。
這意味着您可以通過將關鍵字typedef
替換為例如static
來理解typedef
聲明。 如果static
聲明聲明了某個類型的 object(或函數),則相應的typedef
聲明會創建具有相同名稱和類型的類型定義。 所以這:
static int foo;
創建一個int
類型的 object foo
(具有 static 存儲持續時間),而這:
typedef int foo;
創建一個類型名稱foo
,它是類型int
的別名。
因此,如果您的聲明是:
static Value (*NativeFn)(int argCount, Value* args);
它會將NativeFn
定義為指向函數的指針 object(function 返回Value
類型的結果)。 將static
替換為typedef
意味着NativeFn
是一個類型名稱,它引用了相同的指向函數的類型。
記住typedef
不會創建新類型也很重要。 它為現有類型創建一個新名稱。
考慮這樣的記錄
Value (int argCount, Value* args)
它表示一個 function 類型,它具有返回類型Value
以及int
和Value *
類型的兩個參數。
標識符Value
在其他地方聲明,例如可以是類型的別名。
要將指針類型的別名引入 function 類型,您可以編寫
typedef Value (*NativeFn)(int argCount, Value* args);
因此,例如,如果您有 function
Value some_function(int argCount, Value* args);
然后您可以使用 typedef 別名定義通過以下方式聲明指向此 function 的指針
NativeFn pointer_to_some_function = some_function;
出色地,
typedef (*NativeFn)(int argCount, Value* args)
方法
NativeFn
是一個function type(can also be called "a pointer to function" or "function pointer")
,它返回Value
並接受一個int
和一個Value *
作為 arguments。
如果您對我們為什么以及如何使用它感到不安,那么請閱讀下面的代碼,特別是注釋,您會清楚(*NativeFn)(int argCount, Value* args)
的含義以及如何使用它:
#include <stdio.h>
// note: "Value" is a type declared earlier
// for discussions sake we're declaring our own Value type
typedef struct __value {
int x, y;
} Value;
// now, the following tells us that:
// "NativeFn" is some function type that returns "Value"
typedef Value (*NativeFn)(int argCount, Value* args);
// okay, see how to use "NativeFn"
// for use "NativeFn" we've to declare some function first
// which takes two arguments same as "NativeFn" and return "Value"
Value someFun(int argCount, Value* args) {
// do something argCount and args
// at last it should return some "Value" type
Value v = {2, 3};
return v;
}
int main() {
// now its time to use "NativeFn"
NativeFn fun;
fun = someFun; // notice we can use fun as a variable and assign a
// function to it, which must take arguments same as "NativeFn" and returns "Value" type
Value input = {10, 12};
Value output = fun(1, &input); // note, we're calling "fun", not "someFun"
// it'll output 2, 3, cause we're returning Value v = {2, 3} from "someFun"
printf("(x, y): %d, %d\n", output.x, output.y);
return 0;
}
如果您有任何問題,請在評論中問我...
typedef
工具用於為類型創建別名。 例如,
typedef int *iptr;
創建名稱iptr
作為類型int *
的同義詞,因此您可以使用聲明指針
iptr p, q;
而不是寫
int *p, *q;
C 聲明語法比大多數人意識到的要復雜一些,尤其是在涉及指針的情況下。 基本規則是:
T *p; // p is a pointer to T
T *a[N]; // a is an array of pointer to T
T *f(); // f is a function returning pointer to T
T (*a)[N]; // a is a pointer to an array of T
T (*f)(); // f is a pointer to a function returning T
T const *p; // p is a pointer to const T
const T *p; // same as above
T * const p; // p is a const pointer to T
事情可能會變得任意復雜 - 你可以擁有指向函數的 arrays 指針:
T (*a[N])();
或函數返回指向 arrays 的指針:
T (*f())[N];
甚至更嚴重的暴行。 當混合有參數時,Function 指針聲明變得更加丑陋; 幸運的是,您只需要在聲明中列出參數類型,而不是名稱:
typedef Value (*NativeFn)(int, Value*);
使事情更容易理解。
該聲明NativeFn
創建為類型“指向 function 的指針采用int
和Value *
並返回Value
”的同義詞。
假設您有一個 function 定義為
Value foo( int argcCount, Value *args )
{
Value result;
...
return result;
}
如果你想創建一個指向這個名為fptr
的 function 的指針,你通常將它聲明為
Value (*fptr)(int, Value *) = foo;
但是,上面的typedef
聲明允許您編寫
NativeFn fptr = foo;
說了這么多,請謹慎使用typedef
。 問題在於,雖然它可以創建一種更易於閱讀的方式來聲明某些項目,但它也隱藏了一些潛在有用的信息。 例如,盡管上面有iptr
示例,但最好不要將指針隱藏在 typedef 后面——如果有人需要在p
或q
上使用一元*
運算符才能正確使用它們,那么這些信息需要在這些項目的聲明中. 如果有人需要調用fptr
指向的東西,那么他們需要知道返回類型是什么、參數的數量和類型等,但是使用 typedef 名稱的聲明中缺少所有這些信息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.