200 likes | 350 Views
Arrays as Parameters. Introduction - Arrays can be passed as parameters to functions - some important (and annoying!) differences with passing other types of parameters - Main difference: - Arrays are always passed by reference - don't need (and don't use) an &
E N D
Arrays as Parameters
Introduction - Arrays can be passed as parameters to functions - some important (and annoying!) differences with passing other types of parameters - Main difference: - Arrays are always passed by reference - don't need (and don't use) an & - this is the only way to return an array - Modify a parameter
Arrays as Parameters // Example: copy one array of chars to another const int arrsize= 50; void CopyArray(char[], char[]); void main() { char a[arrsize], b[arrsize]; int i; for (i= 0; i< arrsize; i++) b[ i]= 'Z'; CopyArray( a, b); // copy contents of b to a } Prototype: array parameters do not include size
Arrays as Parameters // Copy array y to x void CopyArray( char x[], char y[]) { int i; for (i= 0; i< arrsize; i++) x[i]= y[i]; } No & necessary: x, y are always reference parameters Don't include array size in function header either: Size is not passed!!! Modifications to x are propagated to actual parameter of caller Using the global constant arrsize
Arrays as Parameters - Often compute with arrays of different sizes - don't want to use a global constant for size - Solution: - Pass the size as an additional parameter // Copy array y to x void CopyArray( char x[], char y[], int sz) { int i; for (i= 0; i< sz; i++) x[ i]= y[ i]; } sz is the size of the arrays x, y
Arrays as Parameters - To call the new CopyArray CopyArray( a, b, arrsize); - arrsize can be local to main instead of global - The fact that 1) arrays are always passed by reference 2) size information is not passed is an annoying limitation of C++ arrays
Constant Parameters - Useful to note explicitly that a parameter is not modified by a function - tells any person reading the code that they can expect parameter to be constant - tells compiler that it can assume parameter to be constant - Value parameters do this in some sense - changes not propagated to actual parameters - For arrays we can't call by value...
Constant Parameters - Declare that a parameter is constant with the const keyword void CopyArray(char x[], const char y[], int sz) { int i; for (i= 0; i< sz; i++) x[ i]= y[ i]; } < See: Example 1>
Array Example: Comparing arrays bool CompareArray(const int x[], const int y[]) { int i; for (i= 0; i< sz; i++) { if (x[ i] != y[ i]) // compare x[i], y[i] return false; // return false if difference found } return true; } both array parameters are declared const:Look, but don't touch!
Passing the Size of an Array - Consider the sz parameter in our copying function: void CopyArray( char x[], const char y[], int sz) { . . . } - What happens if what is passed to sz is bigger than the actual size of the array?
Passing the Size of an Array - Big problems, as we already know - the loop runs off the end of the array - But what happens if the value of sz is smaller than the actual size of the array? - Nothing really - we just don’t use part of it...
Passing the Size of an Array - Why would you ever want to do this? - Suppose we want to read integers into an array until end-of-file, then call a copy function (like that used earlier but for integers) to copy to another array - We don’t know how big the file is, so how can we declare a big enough array to hold it all?
Using only Part of an Array - We can’t have arrays that grow on the fly (there are data types like this that we will see later) - We have no choice at the moment other than setting an arbitrary bound that will be bigger than we need - e. g. say that we will handle any set of values up to 500, and declare our arrays that size.
Using only Part of an Array - Now suppose we write some code to read in the data values... we don’t know how many items there will be in a given file, but we can count them as we read
Using only Part of an Array const int arrsize= 500; int list[arrsize]; // the array will hold up to 500 values int count = 0; // counter to keep track of the number // of slots filled up in the array ifstream myfile; int item; // item from the file myfile. open(“eg1. txt”); myfile >> item; while (myfile && count < arrsize) { // stop @ end of file or no room list[ count] = item; // stick the item in the array count++; myfile >> item; } When loop quits count-1 contains last slot used in array
Using only Part of an Array - Suppose our file had 250 numbers in it... here’s what we’d have: List 0249499 Slots 0 through 249 will have the data we read in, while 250 through 499 will have whatever was in memory when we declared the array - garbage! The variable count will have the value 250 - one more than the last slot we’ve used
Using only Part of an Array So if we wanted to call our copy array function: void CopyArray( int x[], const int y[], int sz) { int i; for (i= 0; i< sz; i++) x[i]= y[i]; } Note the modification - we copy up to and including slot sz because it’s a real slot and not just the absolute size of the array (this also uses integer arrays now)
Using only Part of an Array - So if we wanted to call our copy array function: void CopyArray( int x[], const int y[], int sz) { int i; for (i= 0; i<sz; i++) x[ i]= y[ i]; } - We could pass count (250) to it’s size parameter, and the loop would run up to and including 249 (count-1), copying only the part we used
Using only Part of an Array • - That is, if after reading the data in, we used the following statements: • int list2[arrsize]; // copied array • CopyArray( list2,list, count); • - Only the first 249 items would be copied • - the remainder of list2 would be whatever junk was there before • - Similarly, we could print only the used portion, or sort only the used portion • - we just have to remember where the used portion ends!
Using only Part of an Array - This is commonly done - The only downside is that there will usually be wasted space - we always declare the full array even if we use all of it only rarely - Also a problem where we don’t know how big to make the array - We will discuss how to solve these problems later