1. ## While Loop Kills my Pointer Value. Please help.

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!

2.  Rather, pointers have addresses as values. If you mean the object they point to you have to dereference. [/edit]
Code:
`void extreme( int ar[], int count, int *most, int *least );`
One thing to keep in mind is that a pointer is not required to be used by the calling code only because the function accepts pointers. The main function can also pass in the address of local variables instead.

3. I'm not giving the pointers something to point to at line 29? If that's not the case, doesn't malloc give the pointers something to point to?

4. Originally Posted by Kurtanius21
I'm not giving the pointers something to point to at line 29? If that's not the case, doesn't malloc give the pointers something to point to?
OK, I made a mistake.

Reading your code more carefully I made edits to my last post, so refer to that.

The problem is most likely here, where you meant to write
Code:
`printf("%i %i\n", *most, *least);`
If you don't write that, the result looks like a memory address. Except that's not even how you print memory addresses.

5. As whiteflags said, the way you calling the function from main seems to be the problem.

Also why do you cast what malloc returns?

6. I entered in your suggested modification Whiteflags. Least gives me zero. It works. Most is still faulty. here's what gdb reads.

37 if ( ar[i] > *most )
1: *most = 9
(gdb) s
38 *most = ar[i];
1: *most = 9
(gdb) s
34 while ( i < count )
1: *most = 134520856

I removed the type cast at malloc for most. No help.

7. Fence post error you can run off end of array with i equal to count.

Code:
```  while ( i < count )
{
i++;```
Tim S.

8. So if I add to the while loop check condition

Code:
`while ( i < count - 1 )`
I get the value 8. But 9 exists at the end of the array. gdb showed that. How can I get the 9 to print?

9. Maybe i am wrong, but are you sure you want to call the function like this?
Code:
`extreme( ar, i, most, least );`
Normally we pass addresses like this
Code:
`extreme( ar, i, &most, &least );`

10. Originally Posted by Kurtanius21
So if I add to the while loop check condition

Code:
`while ( i < count - 1 )`
I get the value 8. But 9 exists at the end of the array. gdb showed that. How can I get the 9 to print?
Edit2: Write a normal loop with the increment at the end; instead of subtracting one from count.

Edit: Use a valid for loop

Tim S.

11. Originally Posted by std10093
Maybe i am wrong, but are you sure you want to call the function like this?
Code:
`extreme( ar, i, most, least );`
Normally we pass addresses like this
Code:
`extreme( ar, i, &most, &least );`
`int ar[10], i = 0, *most, *least;`