You have a problem in your code. Change
This is done because
scanf expects an argument of type
int* but you provide argument
arr[i] which is of type
int. Also add a check that ends the program if user inputs a number which is greater than 10 for the first
There can be two reasons.
Case 1 [Much likely for _always_]
Simple. Because your
if(arr[i]==n) condition is not met, and
i<m became false. It came out of
for() loop and hence,
case 2 [Less likely for _always_]
By chance, the value of
n is present at the 21st location [index 20] in the input array.
Apart from the coding aspect, did you understand what’s the logical purpose of this function? If not, begin with that. It searches for a specific value in an array of given length, and if no element of the array matches that value, it returns 20.
Now analyze your case, based on your input.
After seeing the complete code, as Mr. CoolGuy has pointed out, use
Just for more reference, you can look at Chapter 22.214.171.124, paragraph 12 ,
%d format specifier, which goes like
… The corresponding argument shall be a pointer to signed integer.
In your code,
arr[i] is of type
int. What you need is a
int *, i.e.,