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.

int main(void)<br />

{<br />

int i;<br />

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

}<br />

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

{<br />

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

}<br />

bubble(arr,10);<br />

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

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

{<br />

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

}<br />

return 0;<br />

void bubble(int *p, int N)<br />

{<br />

int i, j, t;<br />

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

{<br />

for (j = 1; j *n);<br />

}<br />

Observa los cambios. Estamos ahora pasando un apuntador a un <strong>en</strong>tero (o a un arreglo <strong>de</strong> <strong>en</strong>teros) a bubble().<br />

Y <strong>de</strong>s<strong>de</strong> <strong>de</strong>ntro <strong>de</strong> bubble estamos pasando apuntadores a los elem<strong>en</strong>tos que queremos comparar <strong>de</strong>l arreglo a<br />

nuestra función <strong>de</strong> comparación. Y por supuesto estamos <strong>de</strong>srefer<strong>en</strong>ciando estos apuntadores <strong>en</strong> nuestra<br />

función compare() <strong>de</strong> modo que se haga la comparación real <strong>en</strong>tre elem<strong>en</strong>tos. Nuestro sigui<strong>en</strong>te paso será<br />

convertir los apuntadores <strong>en</strong> bubble() a apuntadores sin tipo <strong>de</strong> tal modo que la función se vuelva más<br />

ins<strong>en</strong>sible al tipo <strong>de</strong> datos a or<strong>de</strong>nar. Esto se muestra <strong>en</strong> el programa bubble_4.c<br />

bubble_4.c<br />

/* Program bubble_4.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 />

41

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

Saved successfully!

Ooh no, something went wrong!