En C, ils est courant d'utiliser les pointeurs de fonctions et de les regrouper en tableaux, comme dans cet exemple, trouvé sur internet :
Code : Tout sélectionner
1 - #include <stdio.h>
2 -
3 - int sum(int x, int y) {return(x + y);}
4 - int sub(int x, int y) {return(x - y);}
5 - int mult(int x, int y) {return(x * y);}
6 - int div(int x, int y) {if (y != 0) return (x / y); else return 0;}
7 -
8 - int main()
9 - {
10 - int x, y, choice, result;
11 - int (*ope[4])(int, int); // <--- le tableau de pointeurs de fonction
12 - ope[0] = sum; // <--- et son remplissage
13 - ope[1] = sub;
14 - ope[2] = mult;
15 - ope[3] = div;
16 - printf("Enter two integer numbers: ");
17 - scanf("%d%d", &x, &y);
18 - printf("Enter 0 to sum, 1 to subtract, 2 to multiply, or 3 to divide: ");
19 - scanf("%d", &choice);
20 - result = ope[choice](x, y); // <--- Utilisation d'un élément du tableau
21 - printf("%d", result);
22 - return 0;
23 - }
Code : Tout sélectionner
EnableExplicit
Macro HexFormat(Prefix, Value, Size)
Prefix + RSet(Hex(Value), Size, "0")
EndMacro
Procedure.s fChar0Decoder(aChar.a, sString.s)
Debug "fChar0Decoder, Char = " + HexFormat("0x", aChar, 2) + " = '" + Chr(aChar) + "'"
Debug "String = '" + sString + "', String Length = " + Len(sString)
ProcedureReturn "fChar0Decoder"
EndProcedure
Procedure.s fChar1Decoder(aChar.a, sString.s)
Debug "fChar0Decoder, Char = " + HexFormat("0x", aChar, 2) + " = '" + Chr(aChar) + "'"
Debug "String = '" + sString + "', String Length = " + Len(sString)
ProcedureReturn "fChar1Decoder"
EndProcedure
Procedure.s fChar2Decoder(aChar.a, sString.s)
Debug "fChar0Decoder, Char = " + HexFormat("0x", aChar, 2) + " = '" + Chr(aChar) + "'"
Debug "String = '" + sString + "', String Length = " + Len(sString)
ProcedureReturn "fChar2Decoder"
EndProcedure
Procedure.s fChar3Decoder(aChar.a, sString.s)
Debug "fChar0Decoder, Char = " + HexFormat("0x", aChar, 2) + " = '" + Chr(aChar) + "'"
Debug "String = '" + sString + "', String Length = " + Len(sString)
ProcedureReturn "fChar3Decoder"
EndProcedure
ProcedureC.i iCharDecoderFuncAddress(Function.i)
ShowCallstack() ; <--- Je voulais voir la pile mais la fenêtre ouverte ici n'est pas celle que j'attendais
Debug "Function.i = " + HexFormat("0x", Function, 16)
ProcedureReturn #Null
EndProcedure
Define iProcedureIndex.i, iResult.i, sResult.s
Dim fCharDecoder.i(3)
sResult = fChar0Decoder(75, "Test0") : Debug "Direct Call : sResult = '" + sResult + ~"'\n"
sResult = fChar1Decoder(76, "Test1") : Debug "Direct Call : sResult = '" + sResult + ~"'\n"
sResult = fChar2Decoder(77, "Test2") : Debug "Direct Call : sResult = '" + sResult + ~"'\n"
sResult = fChar3Decoder(78, "Test3") : Debug "Direct Call : sResult = '" + sResult + ~"'\n"
fCharDecoder(0) = @fChar0Decoder()
fCharDecoder(1) = @fChar1Decoder()
fCharDecoder(2) = @fChar2Decoder()
fCharDecoder(3) = @fChar3Decoder()
For iProcedureIndex = 0 To 3
; fCharDecoder(iProcedureIndex)('N', "Test3") ; <--- Tentative d'utilisation du tableau => Syntax error !
iResult = iCharDecoderFuncAddress(fCharDecoder(iProcedureIndex))
Debug "iResult = " + iResult + ~"\n"
Next
End
Par avance, merci pour votre aide