1

I'm writing a programm to create a roster. Every month has a different number of shifts, which is determined by a function. At the beginning of the programm the user enters the month, the according number of shifts gets calculated in a function and then I want to create a 2-dimensional array with that size. But aparrently I can't initialize an array like this. Can anyone help me out?

As you may have noticed, I'm a very inexperienced beginner, so I apologise for not expressing myself perfectly in advance.

//function to calculate number of shifts
const int getShift(const int month, const int year) {
    ...
    return x;
}

int main(){
int array[getShift(8,2019)[2];
}

I got an error along the lines of "expression did not evaluate to a constant" although that number actually is a constant, or at least I want it to be one...

Thanks in advance for your help!

6
  • Use malloc or calloc rather than array syntax. Commented Aug 11, 2019 at 13:11
  • 3
    @nicomp That is not a good suggestion in modern C++ Commented Aug 11, 2019 at 13:12
  • 3
    A std::vector is most likely what you want. Commented Aug 11, 2019 at 13:13
  • 3
    @nicomp No, don't. Commented Aug 11, 2019 at 13:16
  • 3
    @nicomp "Use malloc or calloc" - In modern (or even ancient) C++, no please don't do that. Use new or (better) a std::vector. malloc and calloc don't call constructors - you almost never want to use them in C++. Commented Aug 11, 2019 at 13:17

2 Answers 2

3

When you need an array with a dynamic size, almost always the best solution in C++ is to use a vector.

#include <array>
#include <vector>

//function to calculate number of shifts
int getShift(int month, int year) {
    ...
    return x;
}

int main() {
    std::vector<std::array<int, 2>> array(getShift(8,2019));
}

Since you need a 2D array, and since regular arrays can't be members of a vector, I've also used a std::array<int, 2> for the second dimension.

Now you can use array pretty much like a regular 2D array, in particular you can use array[i][j] to access individual elements of the 2D array.

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

1 Comment

Thank you, that did solve my problem in an elegant way. Cheers! :)
3

The function must to be declared with the specifier constexpr and accordingly to satisfy the requirements for constexpr functions..

Here is a demonstrative program

#include <iostream>

constexpr int getShift( int x, int y )
{
    return y / x;
}

int main()
{
    int array[getShift(8,2019)][2];

    std::cout << sizeof( array ) / sizeof( *array ) << '\n';
}

Its output is

252

Here is a list of requirements for the body of constexpr functions (C++20)

(3.4) — its function-body shall not enclose (Clause 8)

(3.4.1) — an asm-definition,

(3.4.2) — a goto statement,

(3.4.3) — an identifier label (8.1),

(3.4.4) — a definition of a variable of non-literal type or of static or thread storage duration or for which no initialization is performed.

6 Comments

downvote might come from old c++ users whose experience limited to c-like c++, I guess.
@VladfromMoscow sizeof( array ) / sizeof( *array ) - We have std::size for that these days.
@JesperJuhl Yes we have but not all users use modern compilers.:)
The OP says At the beginning of the programm the user enters the monthAt the beginning of the programm the user enters the month, it's clear that getShift depends on user input so a constexpr is impossible.
@john But in his example he is using constants.:)
|

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.