0

Trying to implement a bool function to check if a certain value exists in an array or not but it checks first index [0] only, I tried the same way manually in main function and it worked but I need it to be in a separate function.

#include <iostream>

using namespace std;

bool ifExist(int arr[], int num){

    for(int i=0; i<sizeof(arr)/sizeof(*arr); i++){    
        if(arr[i] == num){
             return true;
        }
    }
    return false;
}



int main()
{
    int num;
    cin>>num;
    int arr[5]={1,2,3,4,5};
    cout<<ifExist(arr,num);

    return 0;
}
SomethingSomething
  • 11,491
  • 17
  • 68
  • 126
  • 1
    The function parameter `arr` is a pointer and not an array so `i – Jason Aug 07 '22 at 06:15
  • Your `return false` command should be placed after the for loop. Such that only if none of the elements returned `true`, the function will end up returning `false` – SomethingSomething Aug 07 '22 at 06:19
  • What are you using to learn C++? (Or did you learn C++ as part of a datastructures course?). This is not how we write C++ anymore, that would look more like this : https://onlinegdb.com/uy4ZfK5xm – Pepijn Kramer Aug 07 '22 at 06:22
  • Actually my commend is not relevant, it is just an indentation problem. @JasonLiam thank you for teaching me new stuff :) I never knew that even when you define the function parameter as an array with a certain size it decays to a pointer during compilation. – SomethingSomething Aug 07 '22 at 06:24
  • @JasonLiam No longer the recommended way in C++. It's too easy to let the pointer and the size of the array get out of sync (leading to bugs). If it is a fixed size array `printarray(int[5] values)` may be used. But using std::array for fixed length arrays and std::vector for dynamic arrays is much safer and the syntax is less confusing when you want to pass by (const) reference. – Pepijn Kramer Aug 07 '22 at 06:24
  • @SomethingSomething Yes, this is one of the feature that C++ inherited from C. You're welcome :) – Jason Aug 07 '22 at 06:25
  • @PepijnKramer I was just listing few valid options using which this can be solved. You're free to add more(better) options like using `std::array` as you've mentioned. Additionally, in `printarray(int[5] values)` the parameter `values` is still of type `int*`. – Jason Aug 07 '22 at 06:27
  • @JasonLiam Yup, a lot of C++ is about backward compatibility (and a lot of dated training material). Bjarne once said, in C++ there is a cleaner language waiting to get out. (And yes I sometimes forget to answer the question directly and immediately start to redirect... so your comments are more to the point in this case) – Pepijn Kramer Aug 07 '22 at 06:29
  • I just checked [std::array docs](https://en.cppreference.com/w/cpp/container/array). Couldn't understand whether the array itself is allocated on the stack or dynamically on the heap. The fact that it uses templates implies that since the size is known during compilation then like C arrays, it is pre-allocated on the stack, but it is not written in the docs... Maybe it should be obvious? – SomethingSomething Aug 07 '22 at 06:34
  • @PepijnKramer When did `printarray(int[5] values)` become legal C++? – john Aug 07 '22 at 06:38
  • 1
    @SomethingSomething I think this phrase guarantees stack allocation *same semantics as a struct holding a C-style array T[N] as its only non-static data member* – john Aug 07 '22 at 06:40
  • @john It didn't, typos (I have not used that syntax for a long time now) – Pepijn Kramer Aug 07 '22 at 07:15

0 Answers0