'Cpp my index variable for array size in a function is having error
I'm trying to make a function outside main in C++ to search if an element exists inside an array, if so, the function should store the indexes/locations of that element in that array in another array called index.
the problem is, when i try to use the same size for both arrays, the search function tells an error where the size variable must be a constant value, so i tried and this is my code..
#include <iostream>
using namespace std;
int searchForElement(int[], int const, int);
int main()
{
int array[5];
for (int i = 0; i < 5; i++)
array[i] = i * 2;
searchForElement(array, 5, 6);
return 0;
}
int searchForElement(int a[], int const n, int x) {
int index[n];
int ii = 0; //counter for index array
for (int i = 0; i < n; i++) {
if (a[i] == x){
index[ii] = i;
ii++;
}
}
return index[n];
}
Severity Code Description Project File Line Suppression State Error (active) E0028 expression must have a constant value ConsoleApplication1 C:...\repos\ConsoleApplication1\ConsoleApplication1\ConsoleApplication1.cpp 45
how can i make the index array the same size of any given array to this function?
Solution 1:[1]
how can i make the index array the same size of any given array to this function?
Option 1: Replace the function with a function template, and make the size of the array a template parameter. Template parameters are compile time constant.
Option 2: Create a dynamic array. The most convenient way is to use std::vector.
Option 3: Don't create the array in searchForElement. The caller knows how large array they need, so you could let them pass it into the function just like they passed the search array.
return index[n];
Firstly, the index n would be outside the bounds of the array, so the behaviour of the program will be undefined. Secondly, if your intention is to search for all indices, then returning one of the indices probably isn't what you tried to do.
Solution 2:[2]
Don't you want to make your job easier by taking advantage of some modern features?
Check this out ( C++20 required ):
#include <iostream>
#include <vector>
#include <span>
std::vector< std::size_t > searchForElement( const std::span<const int> array, const int key )
{
std::vector< std::size_t > indices;
indices.reserve( array.size( ) );
for ( std::size_t idx { }; idx < array.size( ); ++idx )
{
if ( array[idx] == key )
{
indices.push_back( idx );
}
}
indices.shrink_to_fit( );
return indices;
}
int main( )
{
std::array<int, 5> array;
for ( std::size_t idx { }; idx < std::size( array ); ++idx )
array[ idx ] = idx * 2;
std::vector< std::size_t > indices { searchForElement( array, 6 ) };
std::cout << "The given 'key' was found in indices:\n";
for ( const auto index : indices )
{
std::cout << index << ' ';
}
}
Summary:
- Use STL containers like
std::array. - Return a
std::vectorcontaining the indices. - Use
std::spanin place of array pointer + size. That way you can pass it astd::vector,std::array, raw array, etc.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 |
