// @topic W04-0-030 C/C++ pointers and arrays
// @brief Primitive Aggregate Types in C/C++, important things to know about arrays

// CIS-123 Week 05 Example program
#include <iostream>
#include <cstdlib>

/*
-----------------------------------
Primitive Aggregate Types in C/C++
-----------------------------------

"abcdef"                // literal string

int value[7];           // declaration array of 7 integers
int value[7] = {0};     // initializer list
int value[] = {10, 20, 30, 40, 50, 60, 70};
cout << value[5];       // we use subscript (or index) when referring to
                        // specific elements:

What are important things to know about arrays?

1. arrays are zero-based:
   value[0], value[1], value[2], ... value[6]

2. size of an array <<==== IMPORTANT and may be CONFUSING
    sizeof(array) returns size of array in bytes,
                  BUT ONLY in scope where array was declared
                  This is due to EXTREME primitiveness of C array

3. sizeof( array ) / sizeof( int ) -- returns number of elements

*/

void display_array( int* parray, int array_size );
void display_string( char* pstr );

int main()
{
    std::cout << "abcdef"; // NULL character appended automatically
    char greeting[] = "hello";
    display_string( greeting );

    const int DAYS_IN_WEEK = 7;
    int value[DAYS_IN_WEEK] = { 10, 20, 30, 40, 50, 60, 70 };
    display_array( value, DAYS_IN_WEEK ); // no need to use address-of operator& here
    display_array( value, sizeof( value ) / sizeof( int ) ); // calculates number of elements
    system( "pause" );
    return 0;
}

void display_string( char* pstr ) {
    *pstr = 'H'; // this fails for read only string literals such as "hello"
    std::cout << pstr;
}//display_string

void display_array(int* parray, int array_size) {
    // parray[0] parray[1]
    // *parray  /
    //   /     /
    // 10,   20,   30,   40,   50,    60,   70

    // few examples of obtaining addresses of array elements
    int* pone = parray;         // points to the initial (first) array element
    int* ptwo = parray + 1;     // points to the second element
    int* pthree = &parray[2];   // points to the third element

    std::cout << "number of elements is " << array_size << "\n";

    int idx = 0;
    for ( ; idx < array_size; ++idx ) {
        std::cout << "value [ " << idx << " ] = " << parray[ idx ] << "\n";
    }
}//display_array