Algorithmics and C programming Language - Nicolas Gaud
Transcription
Algorithmics and C programming Language - Nicolas Gaud
Algorithmics and C programming Language LO27 –Fall 2015 https://moodle.utbm.fr http://nico.gaud.free.fr Nicolas Gaud Université de Technologie de Belfort-Montbéliard F-90 000 Belfort, France – [email protected] N. Gaud LO27 – F2015 – UTBM Pointers on function Université de Technologie de Belfort-Montbéliard F-90 000 Belfort, France – [email protected] Pointers on Function 2 Pointers on Function The function name is a symbolic constant (a pointer) referencing the first executable instruction of this function in the CS. For declaring a function’s pointer: <returnedType> (*functionName)(<parameters>); Example 1 2 3 4 i n t swap ( i n t a , i n t b ) { . . . } i n t (∗ p t r F c t ) ( i n t a , i n t b ) ; p t r F c t = swap ; swap ( 1 2 , 2 3 ) ; p t r F c t ( 1 2 , 2 3 ) ; N. Gaud LO27 – F2015 – UTBM Pointers on Function: A concrete example Pointers on Function: A concrete example A function sorting an array of integer but externalizing the comparison problem in a dedicated function. N. Gaud LO27 – F2015 – UTBM 3 Pointers on Function: A concrete example 4 1 t y p e d e f enum { FALSE , TRUE } b o o l ; 2 3 4 5 6 7 8 9 10 11 12 void s o r t A r r a y ( i n t ∗ array , unsigned short size , bool (∗ comparator ) ( i n t a , i n t b ) ) { unsigned short i , j ; f o r ( i =0; i <s i z e −1; i ++) { f o r ( j=i +1; j <s i z e ; j ++) { i f ( ! comparator ( a r r a y [ i ] , a r r a y [ j ] ) ) swap ( a r r a y+i , a r r a y+j ) ; } } } 13 14 15 16 bool comparatorAscOrder ( i n t a , i n t b) { r e t u r n ( a<=b ) ; } 17 18 19 20 21 v o i d main ( ) { i n t t [ ] = {23 ,34 ,1 ,456 ,3 ,234455 ,3 , −134}; s o r t A r r a y ( t , 8 , comparatorAscOrder ) ; } N. Gaud LO27 – F2015 – UTBM Thank you for your attention. . . Université de Technologie de Belfort-Montbéliard F-90 000 Belfort, France – [email protected] Appendix Université de Technologie de Belfort-Montbéliard F-90 000 Belfort, France – [email protected] Author: Dr. Nicolas GAUD Associate Professor Laboratoire Systèmes and Transport (IRTES-SET) Institut de Recherche sur les Transports, l’Énergie et la Société Université de Technologie de Belfort-Montbéliard, France Topics: Multiagent systems, Multiagent-based simulation, Agent-Oriented Software Engineering. Web page: Email: http://multiagent.fr/People:Gaud_nicolas [email protected] See also his open-source contributions: http://www.sarl.io http://www.aspecs.org http://www.janus-project.org http://www.arakhne.org N. Gaud LO27 – F2015 – UTBM i