Array
Array and Size
#include <iostream>
int main(int argc, char *argv[])
{
const int size = 10;
int array[size];
//initialize array
for(int i = 0; i < size; i++)
array[i] = i*i;
//display array
for(int i = 0; i < size; i++)
std::cout<<array[i]<<" ";
std::cout<<std::endl;
//display array size
std::cout<<"Memory size: "<<sizeof(array)<<std::endl;
std::cout<<"Number of elements: "<<sizeof(array)/sizeof(*array)<<std::endl;
return 0;
}
- Subscript number starts at zero
Bounds Checking
#include <iostream>
int main(int argc, char *argv[])
{
const int size = 5;
int array[size];
for(int i = 0; i < size; i++)
array[i] = 10*i;
for(int i = 0; i < 10; i++)
std::cout<<array[i]<<std::endl;
return 0;
}
C++ does not perform array bounds checking
Array Initialization
#include <iostream>
void display(int array [], int size)
{
for(int i = 0; i < size; i++)
std::cout<<array[i]<<" ";
std::cout<<std::endl;
}
int main(int argc, char *argv[])
{
const int size = 5;
//array initialization
int array[size] = {10, 20, 30, 40, 50};
display(array, sizeof(array)/sizeof(*array));
//partial array initialization
int array2[size] = {10, 20};
display(array2, size);
//empty initialization
int array3[size] = {};
display(array3, size);
//no initialization
int array4[size];
display(array4, size);
//implicit array sizing
int array5[] = {10, 20, 30, 40};
display(array5, 4);
return 0;
}
- if an array is partially initialized, the uninitialized elements will be set to zero
- Initialize an array without providing size, array size is the number of items in the initialization list
Assignment
#include <iostream>
void display(int array [], int size)
{
for(int i = 0; i < size; i++)
std::cout<<array[i]<<" ";
std::cout<<std::endl;
}
int main(int argc, char *argv[])
{
const int size = 5;
int array[size] = {10, 20, 30, 40, 50};
int array2[size];
std::cout<<"Address of array: "<<array<<std::endl;
std::cout<<"Address of array2: "<<array2<<std::endl;
//array2 = array;//compile error
for(int i = 0; i < size; i++)
array2[i] = array[i];
display(array2, size);
return 0;
}
- array2 = array, does not work
- Assign the individual elements in the array one by one
- The name of an array is the beginning memory address of the array
- Cannot change the starting memory address of an array
Variable Size of Array
#include <iostream>
int main(int argc, char *argv[])
{
int size;
std::cout<<"Enter the size of the array: "<<std::endl;
std::cin>>size;
int array[size];
std::cout<<sizeof(array)/sizeof(*array)<<std::endl;
return 0;
}
Pass Array to Function
#include <iostream>
void display(int array[], int s)
{
for(int i = 0; i < s; i++)
std::cout<<array[i]<<" ";
std::cout<<std::endl;
}
void init(int array[], int s)
{
for(int i = 0; i < s; i++)
array[i] = 10*i;
}
int main(int argc, char *argv[])
{
const int size = 5;
int array[size];
init(array, size);
display(array, size);
return 0;
}
- Pass the beginning address of the array to the function
- Array parameters work like reference variables
Two Dimensional Array
#include <iostream>
const int rowSize = 3, columnSize = 4;
void display(int array[][columnSize], int rowSize)
{
for(int i = 0; i < rowSize; i++)
{
for(int j = 0; j < columnSize; j++)
std::cout<<array[i][j]<<" ";
std::cout<<std::endl;
}
}
void init(int array[][columnSize], int rowSize)
{
for(int i = 0; i < rowSize; i++)
for(int j = 0; j < columnSize; j++)
array[i][j] = i*10+j;
}
int main(int argc, char *argv[])
{
int array[rowSize][columnSize];
init(array, rowSize);
std::cout<<"Total size: "<<sizeof(array)<<" "<<sizeof(array)/sizeof(*array[0])<<std::endl;
std::cout<<"Row size: "<<sizeof(*array)<<" "<<sizeof(*array)/sizeof(*array[0])<<std::endl;
display(array, rowSize);
return 0;
}
C++ requires the column size to be specified in the function prototype and header because of the way two-dimensional arrays are stored in memory and one row follows another
Template
#include <iostream>
template <class T>
void init(T array[], int size)
{
for(int i = 0; i < size; i++)
array[i] = 10*i;
}
template <class T>
void display(T array[], int size)
{
for(int i = 0; i < size; i++)
std::cout<<array[i]<<" ";
std::cout<<std::endl;
}
int main(int argc, char *argv[])
{
const int size = 5;
double array[size];
init(array, size);
display(array, size);
return 0;
}
Reference
- Starting out with C++, Tony Gaddis