array initialisation inside class

I had to create an array of 16*16 and initialize with values in class

unsigned int* sub_arr[16][16];
sub_arr[0][0] = 0x63;
sub_arr[0][1] = 0x7C;
sub_arr[0][2] = 0x77;
sub_arr[0][3] = 0x7B;
sub_arr[0][4] = 0xF2;
sub_arr[0][5] = 0x6B;
sub_arr[0][6] = 0x6F;
sub_arr[0][7] = 0xC5;
sub_arr[0][8] = 0x30;
sub_arr[0][9] = 0x01;
sub_arr[0][10] = 0x67;

Errors:ISO C++ forbids declaration of ‘sub_arr’ with no type
../design/hci_top.cpp:58: error: ISO C++ forbids initialization of member ‘sub_arr’
../design/hci_top.cpp:58: error: making ‘sub_arr’ static
../design/hci_top.cpp:58: error: invalid in-class initialization of static data member of non-integral type ‘int [0][0]’


is it possible to create array like above as it is static and class is dynamic?
then what is alternate of creating memory locations of 16*16 and initialised with values?

Regards
cam
Last edited on
all initialization should be done in constructor
C++11 allows in-class member initialization:
http://www.stroustrup.com/C++11FAQ.html#member-init

So if your tools are C++11 compliant (recent versions of GCC, Clang and Visual Studio) then you should be able to do something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Example
{
public:
    unsigned int sub_arr[16][16] {
        // sub_arr[0][x]
        {
            0x63, 0x7C, 0x77, 0x7B,
            0xF2, 0x6B, 0x6F, 0xC5,
            0x30, 0x01, 0x67 // ...
        }

        // sub_arr[1][x] ...
        // sub_arr[2][x] ...
    };
};

#include <iostream>

int main()
{
    Example e;

    std::cout << std::hex << e.sub_arr[0][2] << std::endl;
}


You also make your own life harder by using pointers to unsigned int.

This is because you are normally not supposed to give "custom" values to pointers, so somewhere you will need to uglify your code with:

reinterpret_cast<unsigned int *>.
What is the value of x, as i want its value from 0 .....15. Please throw some more light.

class Example
{
public:
unsigned int sub_arr[16][16]
{
// sub_arr[0][x]
{
0x63, 0x7C, 0x77, 0x7B,
0xF2, 0x6B, 0x6F, 0xC5,
0x30, 0x01, 0x67, 0x56
}

//sub_arr[1][x]
{
0x63, 0x7C, 0x77, 0x7B,
0xF2, 0x6B, 0x6F, 0xC5,
0x30, 0x01, 0x67, 0x56
}


// sub_arr[2][x] ...
};
};

#include <iostream>

int main()
{
Example e;

std::cout << std::hex << e.sub_arr[0][2] << std::endl;
}


My code like below, which i had to develop now, So i had to write 16 times as done in previous reply?

unsigned int sub_arr [16][16];
sub_arr[0][0] = 0x63;
sub_arr[0][1] = 0x7C;
sub_arr[0][2] = 0x77;
sub_arr[0][3] = 0x7B;
sub_arr[0][4] = 0xF2;
sub_arr[0][5] = 0x6B;
sub_arr[0][6] = 0x6F;
sub_arr[0][7] = 0xC5;
sub_arr[0][8] = 0x30;
sub_arr[0][9] = 0x01;
sub_arr[0][10] = 0x67;
sub_arr[0][11] = 0x2B;
sub_arr[0][12] = 0xFE;
sub_arr[0][13] = 0xD7;
sub_arr[0][14] = 0xAB;
sub_arr[0][15] = 0x76;

sub_arr[1][0] = 0xCA;
sub_arr[1][1] = 0x82;
sub_arr[1][2] = 0xC9;
sub_arr[1][3] = 0x7D;
sub_arr[1][4] = 0xFA;
sub_arr[1][5] = 0x59;
sub_arr[1][6] = 0x47;
sub_arr[1][7] = 0xF0;
sub_arr[1][8] = 0xAD;
sub_arr[1][9] = 0xD4;
sub_arr[1][10] = 0XA2;
sub_arr[1][11] = 0xAF;
sub_arr[1][12] = 0x9C;
sub_arr[1][13] = 0xA4;
sub_arr[1][14] = 0x72;
sub_arr[1][15] = 0xC0;

:
:
up to
sub_arr[15][15] = 0xC0;
Last edited on
I wrote x to help you understand that we're filling one line row of the matrix at a time.
I also used your own values to make it easy to see.

Here's a simpler example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int matrix[2][3];

matrix[0][0] = 1;
matrix[0][1] = 2;
matrix[0][2] = 3;
matrix[1][0] = 4;
matrix[1][1] = 5;
matrix[1][2] = 6;

// equivalent to

int matrix[2][3] {
    {1, 2, 3},
    {4, 5, 6}
};

Last edited on
thanks catfish666,

I had done as below in class

unsigned int sub_arr[2][16]
{
{0x63, 0x7C, 0x77, 0x7B, 0xF2, 0x6B, 0x6F, 0xC5, 0x30, 0x01, 0x67, 0x2B, 0xFE, 0xD7, 0xAB, 0x76},

{0x63, 0x7C, 0x77, 0x7B, 0xF2, 0x6B, 0x6F, 0xC5, 0x30, 0x01, 0x67, 0x2B, 0xFE, 0xD7, 0xAB, 0x76}
};

error: function definition does not declare parameters

Is there something i had to do initialization in constructor?
Last edited on
Please post the entire source code, and also put it in code tags:

[code]int i=0; // example [/code]

Please remember that this is a C++11 feature.
If your compiler is not new enough, it may not work. You need to use GCC 4.8+ or Visual Studio 2013.
k, then in old gcc versions, How it would be implemented
In that case, you need to do it the hard way, by moving assignment operations inside the constructor.

Relevant threads on StackOverflow if you want to read them:
http://stackoverflow.com/questions/8682296/how-to-initialize-array-elements-by-using-initializer-list
http://stackoverflow.com/questions/5602030/how-can-i-use-member-initialization-list-to-initialize-it

1
2
3
4
5
6
7
8
9
10
11
12
13
class Example
{
public:
    unsigned int sub_arr[16][16];

    Example()
    {
        sub_arr[0][0] = 0x63;
        sub_arr[0][1] = 0x7C;

        // ...
    }
};
Last edited on
> then in old gcc versions, How it would be implemented

Move the initialization of the array into a separate function which is called from within the constructors.
Or else overloaded constructors would create a maintenance hazard.

1
2
3
4
5
6
7
8
9
10
11
struct A
{
    A() { init_array() ; /* ... */ }
    A( int ) { init_array() ; /* ... */ }
    A( const char* ) { init_array() ; /* ... */ }

    private:
        enum { N = 16 };
        unsigned int array[N][N];
        void init_array() ;
};


1
2
3
4
5
6
7
8
9
10
void A::init_array()
{
    static const unsigned int array_init[N][N] =
    {
        { 0, 1, 2, 3, 4, 5, 6, 7, 8 },
        { 16, 17, 18},
        // ...
    };
    std::memcpy( array, array_init, sizeof(array) ) ;
}
Topic archived. No new replies allowed.