17.05.2013 Views

Tutorial de Apuntadores y Arreglos en C - Cimat

Tutorial de Apuntadores y Arreglos en C - Cimat

Tutorial de Apuntadores y Arreglos en C - Cimat

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CAPITULO 10: APUNTADORES A FUNCIONES<br />

Hasta este punto hemos discutido el uso <strong>de</strong> apuntadores con objetos <strong>de</strong> datos. C permite también la<br />

<strong>de</strong>claración <strong>de</strong> apuntadores a funciones. Los apuntadores a funciones ti<strong>en</strong><strong>en</strong> variedad <strong>de</strong> usos y algunos <strong>de</strong><br />

estos serán expuestos aquí.<br />

Consi<strong>de</strong>remos el sigui<strong>en</strong>te problema real: T<strong>en</strong>emos que escribir una función que sea capaz <strong>de</strong> or<strong>de</strong>nar<br />

virtualm<strong>en</strong>te cualquier colección <strong>de</strong> datos que pueda ser cont<strong>en</strong>ida <strong>en</strong> un arreglo. Sea este un arreglo <strong>de</strong><br />

ca<strong>de</strong>nas, <strong>de</strong> <strong>en</strong>teros, flotantes, e incluso estructuras.<br />

El algoritmo <strong>de</strong> or<strong>de</strong>nación pue<strong>de</strong> ser el mismo para todos. Por ejemplo, pue<strong>de</strong> ser un simple algoritmo <strong>de</strong><br />

or<strong>de</strong>nación por el método <strong>de</strong> la burbuja, o el mucho más complejo algoritmo <strong>de</strong> or<strong>de</strong>nación quick sort o por shell<br />

sort. Usaremos un simple algoritmo <strong>de</strong> burbuja para nuestros fines didácticos.<br />

Sedgewick [1] ha <strong>de</strong>scrito el algoritmo <strong>de</strong> or<strong>de</strong>nación por burbuja usando código C al establecer una función a<br />

la que, una vez que se le ha pasado el apuntador al arreglo, or<strong>de</strong>na el arreglo. Si le llamamos a esta función<br />

bubble(), un programa <strong>de</strong> or<strong>de</strong>nación es <strong>de</strong>scrito por bubble_1.c <strong>en</strong> el código que sigue:<br />

bubble_1.c<br />

/* Program bubble_1.c from PTRTUT10.HTM 6/13/97 */<br />

#inclu<strong>de</strong> <br />

int arr[10] = { 3,6,1,2,3,8,4,1,7,2};<br />

void bubble(int a[], int N);<br />

int main(void)<br />

{<br />

int i;<br />

putchar('\n');<br />

for (i = 0; i < 10; i++)<br />

{<br />

printf("%d ", arr[i]);<br />

}<br />

bubble(arr,10);<br />

putchar('\n');<br />

}<br />

for (i = 0; i < 10; i++)<br />

{<br />

printf("%d ", arr[i]);<br />

}<br />

return 0;<br />

void bubble(int a[], int N)<br />

{<br />

int i, j, t;<br />

for (i = N-1; i >= 0; i--)<br />

{<br />

for (j = 1; j

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!