// Native_Arrays3.cpp : Defines the entry point for the console application. /*_______________________________________________________ W O R K I N G W I T H A R R A Y S P A R T 3 Passing Arrays to Functions Demo Ron Kessler Created 1/7/2017 Native C++ Version This project demonstrates how to pass an array to a function as an argument. When you are working with arrays, you are working with contiguous memory locations. Behind the scenes, pointers are created to locate the first element. This "feels" like and responds like we are passing the array by reference. Updated 1/8/2017 Added sorting the array Run using CTL-F5 */ #include "stdafx.h" #include #include //to sort the array if you want to (1/7/2017) using namespace std; void display (int scores []); //define the array int main () { int scores[5] = { 88, 76, 90, 61, 69 }; //load the array //---want to sort it first? sort (begin(scores), end (scores)); display (scores); //passing just the name no index) creates a pointer to the 1st element. return 0; } //---helper function // In line 38, s[] refers to the same location as line 28 does. //This is analogous to passing something by reference. But passing by reference returns //the value in that mem location. A pointer returns the mem location and that is what we use. void display (int s []) { cout << "Displaying scores: " << "\n"; for (int x = 0; x < 5; x++) //iterate through the array { cout << "Student " << x + 1 << ": " << s [x] << "\n"; } }