2013-11-21 31 views
0

很久以前,我遇到過一些舊的非ansi C編寫的舊代碼,我試圖讓我的頭繞着函數定義。奇怪的C函數定義不是K&R

我能理解最終的結果,但我想完全理解代碼風格..

用途:

ok = ElementFn(lifestyleRollupContribution)(gr, nr, cnt, id, prj, k, f, rnd, base); 

函數定義:

Private Logical ElementFn(lifestyleRollupContribution) 
(
    Real* gross, 
    Real* net, 
    Const Real* contribution, 
    Const Date* investment, 
    Const Date* projection, 
    Const PCode* key, 
    Const PCode* fund, 
    Const PCode* inv_prd_round, 
    Const Date* inv_prd_base_date 
) 
{ 
// stuff 
} 

因此,在這個例子中,我可以看到一個名爲ElementFN的函數,它返回一個'Logical'並且有很多參數。我沒有得到什麼是(lifestyleRollupContribution)它只在你看到它時才使用兩次..但是它在做什麼?它表示什麼 - 我沒有認出。我已經看到Kernighan和Ritchie風格函數聲明的引用,但是這似乎並不是那樣的?

+6

檢查'ElementFn'是否是一個宏。 – DCoder

+1

搜索'ElementFn'的所有頭文件。 –

回答

6

在現代的C中,那隻能是一個宏。例如:

#define ElementFn(name) function_ ## name 
#define Private 
typedef float Logical; 
typedef float Real; 

Private Logical ElementFn(lifestyleRollupContribution)(
    Real gross, 
    Real net 
) { 
    return 2.0f; 
} 

int main(void) { 
    printf("%f", ElementFn(lifestyleRollupContribution)(3.05f, 42.0f)); 
    return 0; 
} 
+0

我想你可能是對的我發現這個'#define ElementFn(x)\t EF ## x'儘管它是討厭的代碼!我相信它在數十年前被寫下時是有道理的!我永遠不會考慮一個宏;主要是因爲我從來沒有理由使用它們!而這一個(如果是的話)有大約200行代碼! –

+0

這是一個宏 - 但我仍然不知道它是如何充分使用的。感謝您指點我正確的方向。 –

+0

您的編譯器是否支持擴展宏?在我看來,這非常有幫助。查看以下問題以獲取gcc中的詳細信息:http://stackoverflow.com/questions/985403/seeing-expanded-c-macros – Lazureus