1

I am facing a problem in trying to fill a multidimensional array m[4][4][3][3]. These are actually 16 3x3 matrices that I already know, which I am trying to fill. What is the correct way to do this? I am getting an error :"16: expected primary-expression before '{' token"

Please help.

#include<iostream>
using namespace std;

int main()
{

    int m[4][4][3][3];//0,1,2,3 HLUT

    m[0][0]={1,0,1,
             1,1,1,
             1,0,1};
    m[0][1]={1,1,1,
             0,1,0,
             1,1,1};
    m[0][2]={1,0,1,
             1,1,1,
             1,0,1};
    m[0][3]={1,1,1,
             0,1,0,
             1,1,1};


    m[1][0]={1,0,0,
             1,0,0,
             1,1,1};
    m[1][1]={0,0,1,
             0,0,1,
             1,1,1};
    m[1][2]={1,1,1,
             0,0,1,
             0,0,1};
    m[1][3]={1,1,1,
             1,0,0,
             1,0,0};


    m[2][0]={1,0,1,
             1,0,1,
             1,1,1};
    m[2][1]={1,1,1,
             0,0,1,
             1,1,1};
    m[2][2]={1,1,1,
             1,0,1,
             1,0,1};
    m[2][3]={1,1,1,
             1,0,0,
             1,1,1};


    m[3][0]={1,1,1,
             0,1,0,
             0,1,0};
    m[3][1]={1,0,0,
             1,1,1,
             1,0,0};
    m[3][2]={0,1,0,
             0,1,0,
             1,1,1};
    m[3][3]={0,0,1,
             1,1,1,
             0,0,1};

}                   
5
  • Visual Studio gives a better error message: error C3863: array type 'int [3][3]' is not assignable Commented Nov 24, 2015 at 5:51
  • Why is it not assignable? Is there another way to do this? Commented Nov 24, 2015 at 5:53
  • I've never tried to do it before, but I don't think it's possible. I can't say for sure, though. Commented Nov 24, 2015 at 5:55
  • 1
    Doesn't that try to assign 9 elements to an array with 3 spaces? I think you need to do m[0][0][0] = {1,1,1}; and so on. You can also try using double braces with m[0][0] = {{1,0,1,1,1,1,1,0,1}};. I'm not near my compiler so I can't test it... Commented Nov 24, 2015 at 5:56
  • See this post: How to declare and initialize in a 4-dimensional array in C Commented Nov 24, 2015 at 5:58

3 Answers 3

1

You can't initialize a multidimensional array like that. If you want to do so do it like this:

#include<iostream>
using namespace std;

int main()
{

    int m[4][4][3][3] = {1,0,1, //1
             1,1,1,
             1,0,1,

             1,1,1,     //2
             0,1,0,
             1,1,1,

             1,0,1,     //3
             1,1,1,
             1,0,1,

            // ...
            // ...

             1,0,1,     //16
             1,1,1,
             1,0,1};


    for(int i=0;i<4;++i)
        for(int j=0;j<4;++j) {
            for(int p=0;p<3;++p) {
                for(int q=0;q<3;++q)
                    std::cout<<m[i][j][p][q];
                std::cout<<std::endl;
            }
            std::cout<<std::endl;
        }
}

Or else use 4 for loops to assign your array. You must always remember that a multidimensional array is nothing but a single dimensional array in memory, so initialize it like a single dimensional only.

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

Comments

1

What is the correct way to do this?

You can use one of the following options:

  1. You can initialize all the elements of the matrix at initialization time.
  2. Initialize the members to 0. Then set the values of the elements one by one.
  3. Use std::vector instead of plain old arrays.
  4. Use std::array instead of plain old arrays.

Method 1: Initialize all elements

int main()
{

   int m[4][4][3][3] =
   {
      1,0,1,
      1,1,1,
      1,0,1,

      1,1,1,
      0,1,0,
      1,1,1,

      1,0,1,
      1,1,1,
      1,0,1,

      1,1,1,
      0,1,0,
      1,1,1,

      1,0,0,
      1,0,0,
      1,1,1,

      0,0,1,
      0,0,1,
      1,1,1,

      1,1,1,
      0,0,1,
      0,0,1,

      1,1,1,
      1,0,0,
      1,0,0,

      1,0,1,
      1,0,1,
      1,1,1,

      1,1,1,
      0,0,1,
      1,1,1,

      1,1,1,
      1,0,1,
      1,0,1,

      1,1,1,
      1,0,0,
      1,1,1,

      1,1,1,
      0,1,0,
      0,1,0,

      1,0,0,
      1,1,1,
      1,0,0,

      0,1,0,
      0,1,0,
      1,1,1,

      0,0,1,
      1,1,1,
      0,0,1
   };

}

Method 2: Assign to individual elements

int main()
{

   int m[4][4][3][3] = {};

   m[0][0][0][0] = 1;
   m[0][0][0][1] = 0;
   m[0][0][0][2] = 1;

   ...

   m[3][3][2][0] = 0;
   m[3][3][2][1] = 0;
   m[3][3][2][2] = 1;

}

Method 3: Use std::vector

#include <vector>

int main()
{
   std::vector<int> m1(3);
   std::vector<decltype(m1)> m2(3, m1);
   std::vector<decltype(m2)> m3(4, m2);
   std::vector<decltype(m3)> m(4, m3);

   m[0][0][0] = {1, 0, 1};
   m[0][0][1] = {1, 1, 1};
   m[0][0][2] = {1, 0, 1};

   ...

   m[3][3][0] = {0, 0, 1};
   m[3][3][1] = {1, 1, 1};
   m[3][3][2] = {0, 0, 1};

   return 0;
}

Method 4: Use std::array

#include <array>

int main()
{
   std::array<int, 3> m1;
   std::array<decltype(m1), 3> m2;
   std::array<decltype(m2), 4> m3;
   std::array<decltype(m3), 4> m;

   m[0][0][0] = {1, 0, 1};
   m[0][0][1] = {1, 1, 1};
   m[0][0][2] = {1, 0, 1};

   ...

   m[3][3][0] = {0, 0, 1};
   m[3][3][1] = {1, 1, 1};
   m[3][3][2] = {0, 0, 1};

   return 0;
}

Comments

0
m[0][0]={1,0,1,
         1,1,1,
         1,0,1};

is effectively attempting to assign a single dimensional array of 9 ints to a a 2-dimensional array of 3 ints.

Change your initial declaration to

int m[4][4][9]

2 Comments

The problem still persists. The error message is still: 16: expected primary-expression before '{' token
That is incorrect. You cannot assign an initialization list to an element of an array even if that element itself is an array.

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.