You set aside memory for an array of 100 integers with int ar[100], and you enter n which is set using scanf("%d", &n). If you enter a number greater than 100 at this stage, your program will seg fault because your loop for (i = 0; i < n ; i++) will try to access ar[100] which is a memory access error (the highest array index available is ar[100 - 1], notice that 100 - 1 < 100). Anyways, you fill n indices of ar in the loop. ptr = &ar just assigns the starting address of ar to ptr. By the way ptr = ar will work too, the & is not necessary. Now you can use ptr the same way you were using ar.
The easiest way to understand the recursion is to go straight to the last call of maximum. But first, understand that you passed ptr to the function which is the same as passing ar (remember, they are the same thing in main since ptr = ar.).
So in the last call to maximum, When n + 1 == 1 (same as n == 0), it returns ar[n] which is ar[0], which is first the number you entered for 'Printing the list' (it was stored in ar[0]).
Now in the second last call to maximum, n + 1 == 1 is false because n = 1 so we go to max = maximum(ar, n - 1). That's the result of the last call to maximum that I just explained, so max has the value of ar[0]. Now you have return ar[n] > max ? ar[n] : max, which is the same as return ar[1] > ar[0] ? ar[1] : ar[0]. That is the same as
if (ar[1] > ar[0]) {
return ar[1];
} else {
return ar[0];
}
And you can see that this returns whichever is bigger, ar[0] or ar[1]. Now for the third last call to maximum, max is the result of the second last call to maximum. And you can see the pattern emerge. You will return whichever is greater: max or ar[n] for all the rest of the calls to maximum, and by the time you get to the first call to maximum, you will have compared all the values in ar to find its maximum and return it.
Also, what Ajay said is right, ar[n] is accessing a value that you never initialized in your loop. You should write int max = maximum(ptr, n - 1) in main to fix that.
a[n]is undefined behavior. You are reading uninitialized values.INT_MINtoINT_MAXand the number of separate function stacks that would be created. (yes, in reality the number will "probably" be much less, but be mindful of what could happen)if (n+1==1)though? Apart from being harder to read,n+1can (theoretically) lead to overflow.if (n + 1 == 1)is just the same asif (n == 0). There are a lot of other silly things in this code. Anyways, depth first recursion is very confusing the first time you see it. I went through the motions of a thorough explanation. I hope that helps.maximum(ptr, n)from main will lead to UB, as Ajay mentioned.