Okay, there were a few bugs. I've produced two versions. One annotated with the bugs and a second with things cleaned up.
Here's the annotated version [please pardon the gratuitous style cleanup]:
/* Finds the largest and smallest elements in an array */
#include <stdio.h>
#define N 10
void max_min(int a[], int n, int *max, int *min);
int
main(void)
{
int b[N],
i,
*big,
*small;
printf("Enter %d numbers: ", N);
for (i = 0; i < N; i++)
scanf("%d", &b[i]);
// BUG1: big/small have not been initialized to point to anything
max_min(b, N, big, small);
printf("Largest: %d\n", *big);
printf("Smallest: %d\n", *small);
return 0;
}
void
max_min(int a[], int n, int *max, int *min)
{
int *p;
// BUG2: because of BUG1 above, this will segfault
// BUG3: because p is never initialized, dereferencing it (via "*p") will
// segfault
*max = *min = *p = a[0];
// BUG4: this will run past the end of the "a" array because there is
// no guarantee of a matching sentinel value (i.e. EOF/-1)
// in fact, using a sentinel is wrong because the "a" array can have _any_
// value (i.e. there is _no_ sentinel value that can be used)
while (*p != EOF) {
// BUG5: when max or min gets set, we're changing what they point to
// but this will _not_ change caller's values
if (*p > *max)
max = p;
else if (*p < *min)
min = p;
p++;
}
}
Here's a cleaned up and working version. In particular, note the change of big/small
in main from int *
to int
with a corresponding change in the call to max_min
:
/* Finds the largest and smallest elements in an array */
#include <stdio.h>
#define N 10
void max_min(int a[], int n, int *max, int *min);
int
main(void)
{
int b[N],
i,
big,
small;
printf("Enter %d numbers: ", N);
for (i = 0; i < N; i++)
scanf("%d", &b[i]);
max_min(b, N, &big, &small);
printf("Largest: %d\n", big);
printf("Smallest: %d\n", small);
return 0;
}
void
max_min(int a[], int n, int *max, int *min)
{
int *p;
int val;
p = &a[0];
*max = *min = *p;
for (int i = 0; i < n; ++i, ++p) {
val = *p;
if (val > *max)
*max = val;
else if (val < *min)
*min = val;
}
}