To answer your first question:
Can this be made more simple, should be able to return max and second max without using struct object.
There are a couple of ways to return two (or more) values from one function in C. The first one, as you noted, is using a struct, this way:
// Define a struct holding two integers
struct Pair_of_int {
int a;
int b;
};
// Define a function returning a `PairOfInt`
struct Pair_of_int my_function(int[] a, int size) {
struct Pair_of_int result;
// Do some work
result.a = /*...*/;
result.b = /*...*/;
return result;
}
This is how you would call such a function in your main function :
struct Pair_of_int pair = my_function(...);
printf("First %d, second %d", pair.a, pair.b);
Note that this is not exactly what you have done in the code you've shown. In your code you have a global instance of your struct max_min that gets updated by the function. What you want is to use the structure as the return value of the function, as shown above.
The other way is to use pointers to return multiple values. Here if you want two ints you have to define your function to accept two pointers to int as arguments and use them to store the values.
void my_function(int[]a, int size, int* out_a, int* out_b) {
// Do some work
*out_a = /*...*/;
*out_b = /*...*/;
}
Here as you can see the function returns nothing. Instead it uses the two pointers out_a and out_b to store the result. This is how you would use it in the main function :
int a; int b;
my_function(array, size, &a, &b);
printf("First %d, second %d", a, b);
You create two variables a and b that will hold the results and pass their address to my_function with the & operator.
How above code can be changed to find maximum (that it does) and minimum value from array
This is shouldn't be too hard if you get the first case working correctly. You have to keep track of the two values separately. You might have to add some more arguments to your function for it to work recursively.
{10, 11, 1}it returns{11,1}what is not a valid pair for maximum and the second maximum. It looks that the code actually returns the minimum and the maximum of the array.