0
Can anyone help me in fix this code of array element search in c
5 ответов
+ 1
I don't see the previous warning about non void function not returning data, I don't see what else to fix.
Well, you can opt to return the index of found element instead of printing it inside the function. In which case, you setup a variable in main function to hold the return value from `searchElement`. Later on you use that variable, and print it inside main function.
int searchElement(int arr[], int n, int key) {
for(int i=0;i<n;i++) {
if(arr[i]==key) {
return i; // return the index
}
}
return -1; // not found
}
int main() {
int pos = searchElement(arr,7, 5);
if(pos == -1)
// print not found
else
// print the returned index here
return 0;
}
(Edited)
+ 1
Function `searchElement` signature shows that it returns an `int`, but you didn't issue a return statement even until the end of the function. Add a return statement, e.g. return the index (if found) or return -1 when not found.
+ 1
Ipang return the value 1 but what to do next to fix the code
+ 1
Ipang oh okay this way the functions will work in easy way thanks 😊
+ 1
You're welcome buddy 👌