而不是寫一個if或一個switch陳述句,像這樣:
if (a == 1)
<some code here 1>
else if (a == 2)
<some code here 2>
else if (a == 3)
<some code here 3>
我想運行這樣的東西:
l[1] = here1;
l[2] = here2;
l[3] = here3;
goto l[a];
here1:
<some code here 1>
here2:
<some code here 2>
here3:
<some code here 3>
是否可以在 C 中執行此操作?
uj5u.com熱心網友回復:
不,不是,但有一個 GCC 擴展。https://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html#Labels-as-Values。
所以你的代碼是:
void *l[3] = {&&here1, &&here2, &&here2};
goto *l[a];
here1:
<some code here 1>
here2:
<some code here 2>
here3:
<some code here 3>
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/392349.html
下一篇:如何重構并使這些if陳述句更好?
