e03-01.cpp

来自「游戏开发数据结构Data Structures for Game Program」· C++ 代码 · 共 61 行

CPP
61
字号
// =======================================================
//  Chapter 3, Example 1
//  Demonstrating Static Arrays.
// =======================================================


// -------------------------------------------------------
// Name:        ArrayFunction
// Description: sets array[0] to 10.
// Arguments:   - p_array: the array
// -------------------------------------------------------
void ArrayFunction( int p_array[] )
{
    p_array[0] = 10;
}



void main()
{
    // declare an array with 10 cells.
    int array1[10];

    // declare x
    int x;

    // set the first cell to 5, then set the second
    // cell to the first cell.
    array1[0] = 5;
    array1[1] = array1[0];

    // DONT EVER DO THIS:
    // this next line of code writes past the end
    // of the array, potentially causing harm.
    // array1[10] = 0;


    // pass the array to a function.
    ArrayFunction( array1 );

    // set cell 5 to 42.
    array1[5] = 42;

    // retrieve the value of cell 5 using 3 different methods.
    // x should be 42 after each operation.
    x = array1[5];
    x = *( array1 + 5 );
    x = 5[array1];



    // declare a second array and initialise it.
    int array2[5] = { 1, 2, 3, 4, 5 };

    // declare a third array and initilise it without a specific size
    int array3[] = { 1, 2, 3, 4, 5, 6 };


    // retrieve the number of cells in array3:
    int size = sizeof( array3 ) / sizeof( int );
}

⌨️ 快捷键说明

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