2
void Data::Paramters()
    
{
    for (int i = 0; i < I; i++)
    {
        mc[i] = new int[K];
        for (int k = 0; k < K; k++)
        {
            mc[i][k] = {{1, 0, 2, 3, 5},{ 4, 2, 2, 1, 3 }, { 4, 3, 4, 1, 3 }, { 3, 5, 6, 4, 2 } };
        }
    }
}

getting "Too Many Initializer Values" error in starting of { 4, 2, 2, 1, 3 } where I=5 and K=4

4
  • mc[i][k] refers to a single 2D array element, while you're trying to assign a whole 2D array. Commented Feb 20, 2022 at 20:59
  • @Salvage I'm pretty sure mc[i][k] refers to a single 1D array element, i.e., a single int. Commented Feb 20, 2022 at 21:00
  • If only we knew that for certain. Commented Feb 20, 2022 at 21:01
  • @JohnFilleau An element is fundamentally the same, no matter the dimension of the array it's in. But that's just arguing about semantics I guess. Commented Feb 20, 2022 at 21:04

2 Answers 2

2

The initializer is for a 4x5 2D matrix, so you should just write:

enum { I = 4, K = 5 };

int mc[I][K] = { { 1, 0, 2, 3, 5 },
                 { 4, 2, 2, 1, 3 },
                 { 4, 3, 4, 1, 3 },
                 { 3, 5, 6, 4, 2 } };
Sign up to request clarification or add additional context in comments.

5 Comments

Probably want to be using constexpr over macros.
@Salvage: indeed, but I am trying to keep the source as simple as possible :)
And that's a noble goal, but I have to admit the enum hack is probably more complex and idiosyncratic than the features "modern" C++11 provides. ^^
What would be the current idiom to define I and K these days? I am not familiar with modern C++ constructions... I gave up on it after ingurgitating the Annotated C++ Reference Manual back in 1990 :)
Slightly depends on the context, inside classes and functions you'd want to use static constexpr std::size_t I = 4; and everywhere else you can just omit the static as constexpr implies const which implies internal linkage at namespace scope. Yeah, C++ has changed a ton over the years (I wasn't there to witness it, just started learning when C++17 was around!), but I think in the large majority of cases it did change for the better. :)
2

The expression mc[i][k] has the type int. It is not an array.

So this assignment statement

mc[i][k] = {{1, 0, 2, 3, 5},{ 4, 2, 2, 1, 3 }, { 4, 3, 4, 1, 3 }, { 3, 5, 6, 4, 2 } };

does not make a sense.

If K is a constant expression then you can allocate and initialize the two-dimensional array the following way

int ( *mc )[K] = new int[I][K]
{ 
    { 1, 0, 2, 3, 5 },
    { 4, 2, 2, 1, 3 }, 
    { 4, 3, 4, 1, 3 }, 
    { 3, 5, 6, 4, 2 } 
}; 

Here is a demonstration program.

#include <iostream>

int main()
{
    const size_t K = 5;
    size_t I = 4;

    int ( *mc )[K] = new int[I][K]
    {
        { 1, 0, 2, 3, 5 },
        { 4, 2, 2, 1, 3 },
        { 4, 3, 4, 1, 3 },
        { 3, 5, 6, 4, 2 }
    };

    for ( size_t i = 0; i < I; i++ )
    {
        for (const auto &item : mc[i])
        {
            std::cout << item << ' ';
        }
        std::cout << '\n';
    }
}

The program output is

1 0 2 3 5
4 2 2 1 3
4 3 4 1 3
3 5 6 4 2

11 Comments

Why is I not const?
@chqrlie Because it is unimportant.
@chqrlie Because variable length arrays is not a standard C++ feature.
OK so K must be a constant expression, but not I. And defining const size_t K = 5; make K a constant expression in C++, whereas it does not in C, but C supports VLAs so it does not matter. Subtile differences :)
Is there a way to change for ( size_t i = 0; i < I; i++ ) to something more trendy like for (const auto &item : mc[i]) ?
|

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.