⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 e05-01.cpp

📁 游戏开发数据结构Data Structures for Game Programmers
💻 CPP
字号:
// =======================================================
//  Chapter 5, Example 1
//  Demonstrating Static Arrays.
// =======================================================




void Function1( int p_array2d[4][5], int p_array3d[2][4][4] )
{
    // do nothing.
    return;
}

// this is exactly the same as function1
void Function2( int p_array2d[][5], int p_array3d[][4][4] )
{
    // do nothing.
    return;
}

// this function will not compile.
// void Function3( int p_array2d[][] )
// {
//     // do nothing.
//     return;
// }
// use this one instead:
void Function3( int p_array[][4] )
{
    // do nothing.
    return;
}

// this function will not compile:
// void Function( int p_array[4][] )
// {
//     // do nothing
//     return;
// }


void main()
{
    // declare 2d, 3d and 4d arrays.
    int array2d[5][5];
    int array3d[4][4][4];
    int array4d[3][3][3][3];


    // initialise a 3x3 array
    int array33[3][3] = { { 1, 2, 3 },
                          { 4, 5, 6 },
                          { 7, 8, 9 } };


    // initialise a 2x2x2 array
    int array222[2][2][2] = { { { 1, 2 },
                                { 3, 4 } },
                              { { 5, 6 },
                                { 7, 8 } } };


    // initialise a 2x2x2x2 array
    int array2222[2][2][2][2] = { { { { 1, 2 },
                                      { 3, 4 } },
                                    { { 5, 6 },
                                      { 7, 8 } } },
                                  { { { 9, 10 },
                                      { 11, 12 } },
                                    { { 13, 14 },
                                      { 15, 16 } } } };

    // initialise a 3x2 array
    int array32[3][2] = { { 1, 2 },
                          { 3, 4 },
                          { 5, 6 } };


    // initialise a 3x2x1 array
    int array321[3][2][1] = { { { 1 },
                                { 2 } },
                              { { 3 },
                                { 4 } },
                              { { 5 },
                                { 6 } } };

    // This is invalid, will not compile:
    // int array[][] = { { 1, 2 },
    //                   { 3, 4 } };

    // do this instead:
    int arrayX2[][2] = { { 1, 2 },
                         { 3, 4 } };



    // put some numbers into the arrays:
    array2d[4][3] = 10;
    array3d[3][1][0] = 15;
    array4d[2][2][1][0] = 20;

    // pass some arrays to functions.
    Function1( array2d, array3d );
    Function2( array2d, array3d );

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -