Monday 27 July 2015

Arrays in C++: Part Two

Let us continue studying arrays, which we started last time. Today, we shall be looking at several examples of code.





Array Size

–Can be specified with constant variable (const)
const int size = 20;
–Constants cannot be changed
–Constants must be initialized when declared
–Also called named constants or read-only variables












Passing Arrays to Functions 

          Specify name without brackets
        To pass array myArray  to myFunction
                              int myArray[ 24 ];
                              myFunction( myArray, 24 );
        Array size usually passed, but not required         
          Useful to iterate over all elements

        Arrays passed-by-reference
        Functions can modify original array data
        Value of name of array is address of first element
          Function knows where the array is stored
          Can change original memory locations
        Individual array elements passed-by-value
        Like regular variables
        square( myArray[3] );
          Functions taking arrays
                                -Function header
          void modifyArray( int b[], int arraySize )
          Function prototype
          void modifyArray( int [], int );
        Names optional in prototype
          Both take an integer array and a single integer
        No need for array size between brackets
          Ignored by compiler
        If declare array parameter as const
          Cannot be modified (compiler error)

                                            void doNotModify( const int [] );







About the Author

Unknown

Editorial Team

Post a Comment

 
Codexify © 2015 - Designed by Templateism.com