Using gdb, I know the while loop kills my pointer value when the check condition is no longer true. Why is this, and how can I remedy this? My main function just prints 124231523 124323232. Main btw, just makes a size 10 array with numbers 0-9 for respective addresses in the matrix. My extreme function is supposed to print the least and greatest numbers in the array 0 and 9. Please help.
Code:
# include <stdio.h>
# include<stdlib.h>
void extreme( int ar[], int count, int *most, int *least );
int main ()
{
int ar[10], i = 0, *most, *least;
most = (int *)malloc(sizeof(int));
least = malloc(sizeof(int));
do
{
ar[i] = i;
i++;
} while ( i < 10 );
extreme( ar, i, most, least );
printf("%i %i\n", most, least);
return(0);
}
void extreme( int ar[], int count, int *most, int *least )
{
int i = 0;
*most = ar[i];
*least = ar[i];
while ( i < count )
{
i++;
if ( ar[i] > *most )
*most = ar[i];
else
if ( ar[i] < *least )
*least = ar[i];
}
}
The while loop is at line 34. Thanks!