I want to create an array inside a function just like this:
int function(int a){
int something[a]{};
return something;
}
but the next error "expression must have a constant value" appear.
I want to create an array inside a function just like this:
int function(int a){
int something[a]{};
return something;
}
but the next error "expression must have a constant value" appear.
Your code doesn't really make sense, you are trying to create an array something
, and then return an array from a function that is supposed to return an int
. Guessing at your intentions,
If you want to return an int
from an array based on index a
, you could:
test.cpp
#include <iostream>
int function(int a){
int something[] = {1,2,3};
return something[a];
}
int main()
{
std::cout << function(1) << std::endl;
}
Compile using $ g++ test.cpp -o test
Run using $ ./test