I have this code that is supposed to find the smallest integer in an array. I am learning how to use pointers in C, and my code will not compile, but I do not know why. Rather than just the answer, I'd like to know why I am doing wrong or how is my thought process wrong.
I thought that *p_min
will return the value inside the address.
The errors I get:
error: invalid type argument of ‘unary *’ (have ‘int’)
Warning: return makes pointer from integer without a cast
p is declared outside the function as int *p;
Code:
int *find_smallest(int a[], int N)
{
int *p_min;
for (p = &a[0]; p < &a[N]; p++)
{
if (*p[1] < *p[0]) {
p_min = &p[1];
return *p_min;
} else {
p_min = &p[0];
return *p_min;
}
}
}