6

So I would like to create an array in a function with the size set by a number coming in as the parameter. Here is an example:

void temp_arr ( const int array_size ) {
     int temp_arr[array_size]; //ERROR array_size needs to be a constant value
    //Then do something with the temp arr
}

Even if the parameter is a const int, it will not work. I would like to not use a global const and not use vectors. I am just curious as I am learning C++. I would like for it to make it so that the array size is different each time the function is called. Is there a solution to this or am I to create a const variable and the array before the function is called?

8
  • int* temp_arr = new int[array_size]; ... delete[] temp_arr; Commented Jul 10, 2016 at 5:40
  • @songyuanyao That's not really the same since it'd be going from static allocation to dynamic allocation. Commented Jul 10, 2016 at 5:46
  • 1
    and not use vectors. I am just curious as I am learning C++. -- So you think that std::vector isn't C++? What is it with vector why so many beginners think they shouldn't or can't use it? Commented Jul 10, 2016 at 6:00
  • @PaulMcKenzie It's not that I don't think that I should use vectors, I know that they are apart of the STL and I do use them in projects. I just want to understand what to do with arrays. It's for the purpose of just learning and I am messing around to have a better understanding of the language. Commented Jul 10, 2016 at 6:05
  • 2
    Arrays are limited and just plain dumb. Trying to flesh out more features out of them is a waste of time, IMO. Commented Jul 10, 2016 at 6:07

5 Answers 5

7

Using a template function:

template<std::size_t array_size>
void temp_arr()
{
    int temp_arr[ array_size ];
    // ...work with temp_arr...
}

You can then call the function with this syntax:

temp_arr<32>(); // function will work with a 32 int statically allocated array

Note

Every call with a different value of array_size will instantiate a new function.

Sign up to request clarification or add additional context in comments.

3 Comments

What if the size came from a variable?
@Dmitri Only if it is a constexpr: constexpr std::size_t sz{ 32 }; temp_arr<sz>(); will work. The point is, it has to be available at compilation.
This worked for me. I am using a const int to pass into the template.
2

When you pass a value in this function, the value is not a constant. Defining an array must be done with a constant value. Although you have used const int array_size, that only creates an integer that is constant within the function. So in a way, if you pass a variable value in the function, it takes it as a variable. Thus it produces an error. Yes, you are to create a constant and pass it during the function call.

3 Comments

I tired to have a const int in main and pass that into the function. And I have also tried to add a const int that is equal to array_size inside the function. Are you saying that I should create a global variable?
@kingcobra1986 You don't have to create a global variable, see my answer.
@kingcobra1986 As shown in user2296177's answer, there is no need to create a global variable.
0

If you don't have issue with memory let me tell you an easy way:-

   void temp_arr ( const int array_size )
   {
       //lets just say you want to get the values from user and range will also be decided by the user by the variable array_size

       int arr[100];   //lets just make an array of size 100 you can increase if according to your need;
       for(int i=0; i<array_size ; i++)
       {
         scanf("%d",&arr[i]);
       }
   }

I know this is not a perfect solution but just a easy way for beginners.

1 Comment

You should include some code to prevent buffer overflows when they give the size greater than 100
0

You can use:

int const size = 10;
int array[size]; 

to create an array in C++. However, you cannot use

void temp_arr ( const int array_size ) {
     int temp_arr[array_size];
}

to create an array unless the compiler supports VLAs as an extension. The standard does not support VLAs.

The const qualifier in an argument type merely makes the variable const in the function -- you can't modify its value. However, the value cannot necessarily be determined at compile time.

For example, you can call the function using:

int size;
std::cout << "Enter the size of the array: ";
std::cin >> size;
temp_arr(size);

Since the value cannot be necessarily be determined at compile time, it can't be used to create an array.

1 Comment

The const qualifier in the argument type isn't "useless"
0

You could use a std::unique_ptr:

void temp_arr(int array_size)
{
    auto temp_arr = std::make_unique<int[]>(array_size);
    // code using temp_arr like a C-array
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.