The pointers to max and min values are initialised exactly the same, yet there is some error which causes the min value to be set to some huge number (a value of memory adress or maximum possible value maybe?). My question is - how is that possible and what can I do to fix it? I will be grateful for your help.
#include <stdio.h>
int row_stats(int(*ptr)[5], int width, int height, int row_id, int** max, int** min, float** avg)
{
*max = &ptr[row_id][0];
for(int i=0;i<width;i++)
{
if(ptr[row_id][i]>**max)
{
*max = &ptr[row_id][i];
}
}
*min = &ptr[row_id][0];
for(int i=0;i<width;i++)
{
if(ptr[row_id][i]<**min){
*min = &ptr[row_id][i];
}
}
// At this point, the value of **min is correct and equal to 16
*avg = &ptr[row_id][0]; // I know the types are conflicting here but if I delete it, the max and avg values stop showing.
int sum = 0;
for(int i=0;i<width;i++)
{
sum = sum+ptr[row_id][i];
}
**avg = sum/width;
}
int main() {
int* max_ptr;
int* min_ptr;
float* avg_ptr;
int a[5][5]={{1,2,3,4,5},{6,7,8,9,10},{11,12,13,14,15},{16,17,18,19,20},{21,22,23,24,25}};
row_stats(a,5,5,3,&max_ptr,&min_ptr,&avg_ptr);
printf("MAX: %d\n", *max_ptr); //here the value pointed by max_ptr is correct (20)
printf("MIN: %d\n", *min_ptr); //here the value pointed by min_ptr is 1099956224
printf("AVG: %f\n", *avg_ptr);
return 0;
}