No one showed you what was wrong with your original code. Here are the minimal changes that make your original code work.
Code:
#include <stdio.h>
void smallest_list(int *p)
{
int smallest = *p;
p++;
for (int i = 1; i < 9; i++)
{
if ( smallest > *p ) // check smallest is greater than next
{
smallest = *p; // yes update smallest
}
//!! increment p outside the if statement !!//
p++;
}
//!! print smallest (not *p) !!//
printf("smallest = %d\n", smallest); // print smallest number in list
}
int main()
{
int a[9] = { 6, 2, 8, 5, 3, 9, 7, 1, 4 };
smallest_list(a);
return 0;
}
It makes more sense to pass in the size of the array, though.
Code:
#include <stdio.h>
void print_smallest_value(int *p, int size)
{
if (size <= 0)
{
printf("no elements in array\n");
return;
}
int smallest = *p++;
while (--size > 0)
{
if (*p < smallest)
smallest = *p;
p++;
}
printf("smallest = %d\n", smallest);
}
int main()
{
int a[] = { 6, 2, 8, 5, 3, 9, 7 };
print_smallest_value(a, sizeof a / sizeof a[0]);
int b[] = { 93, 37, 32, 65, 78, 54, 23, 43, 56, 76, 45, 51 };
print_smallest_value(b, sizeof b / sizeof b[0]);
return 0;
}