Need help, whats wrong with this?
This code is supposed to find what spot a certain number in an array is.
#include<iostream.h>
int Search(int NUM_ELEMENTS,int arr[],int searchele) {
for(int i=0;i<NUM_ELEMENTS;i++) {
if(arr[i]==searchele)
return i;
}
return NULL;
}
int main() {
int arr[] = {1,2,5,4};
int *temp;
temp=Search(4,arr,5);
cout<<*temp;
return(0);
}
Thats my code, its not give an error on line 14 saying
error C2440: '=' : cannot convert from 'int' to 'int *'
can anyone tell me what i'm doing wrong?
Re: Need help, whats wrong with this?
PHP Code:
#include<iostream.h>
int Search(int NUM_ELEMENTS,int arr[],int searchele) {
for(int i=0;i<NUM_ELEMENTS;i++) {
if(arr[i]==searchele)
return i;
}
return NULL; // Not sure this is valid return for int
}
int main() {
int arr[] = {1,2,5,4};
int *temp;
temp=Search(4,arr,5);
cout<<*temp;
return(0); // Should be return 0;
}
Some comments added above. Multiple returns are bad ideas in general, and not sure that NULL is a valid return anyway. Try setting another variable to i in the if statement, then just returning that. Initialize that variable first, though, to a number that wouldn't be in a valid subscript range. That way, you can check for that value after the return in case searchele isn't found.