#include<stdio.h>
void find(int a[], int n, int *min, int *max){
int tmp, i, j;
for(i = 0; i < n; i++){
for(j = i + 1; j < n; j++){
if(a[i] < a[j]){
tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
}
}
*max = *a;
*min = *(a + n);
}
int main(){
int a[100];
int n, *min, *max;
printf("husnegtiinn elementiin toog oruul: ");
scanf("%d", &n);
for(int i = 0; i < n; i++){
scanf("%d", &a[i]);
}
find(a, n, min, max);
printf("ih ni : %d \nbaga ni : %d", *max, *min);
}
I am trying to sort the array from user and get the min and max values in those pointers. I am having hard time figuring out how to pass pointers through functions. Am I missing something here?
*(a + n);is outside of the initialized elements in the array.*(a + n - 1)(or simplera[n-1]) is the last element to which you assigned values.minandmaxare not initialized, so it's illegal to dereference and assign values like*max = *a.