上海交通大学交大密西根 联合学院一 1Ua日 181t UM-SJTU Joint Institute ■ University of Michigan Shanghal Jiao Tong University Array Passed as an Address Passing Arrays as Parameters #define N VALUES 500 main ( Accessing or changing array in the function actually accesses int i; or changes values in main. int values [N VALUES]; ClearIntegerArray(values,N VALUES); main clearIntegerArray 500 values array 498 499
Array Passed as an Address Array Passed as an Address
上海交通大学交大密西根 联合学院一 ◆] 181 UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University Array Passed as an Address When we pass an array "values"in the main to function ClearIntegerArray,what passed between the calling and called function is the array name,its value is the address of its first element.It will Passing Arrays as Parameters not be changed in the called function. #define N_VALUES 500 main() What might be int i; changed is the int values[N VALUES】: content of the array ClearIntegerArray(values,N VALUES); element,including main clearIntegerArray the first one. n 500 0 values array 0 498499 array[i] 0:
Array Passed as an Address Array Passed as an Address • When we pass an array “values” in the main to function ClearIntegerArray, what passed between the calling and called function is the array name, its value is the address of its first element. It will not be changed in the called function. What might be changed is the content of the array element, including the first one
上海交通大学交大密西根 B 联合学院一 ◆] 181t UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University Array Passed as an Address So the function could set all the array elements to zero Passing Arrays as Parameters and then #define N VALUES 500 main() return. int i; The slide int values[N VALUES】: ClearIntegerArray (values,N VALUES): illustrates main the result. values 0 0 0 0 0 498 499
Array Passed as an Address Array Passed as an Address • So the function could set all the array elements to zero and then return. The slide illustrates the result
上海交通大学交大密西根 联合学院· 181 UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University Array in Function Declaration The prototype needs to show that the parameter array is an array,and here is how we do that,with an empty pair of square brackets.You may have expected that we would specify the size,but that's not necessary.If C++ doesn't check to be sure that your array references are in bounds,then it doesn't need to know (in the function) how big it is.It only needs the size when the argument array is declared (in order to know how much memory to set aside). Passing Arrays as Parameters void ClearIntegerArray(int array[],int n);
Array in Function Declaration Array in Function Declaration • The prototype needs to show that the parameter array is an array, and here is how we do that, with an empty pair of square brackets. You may have expected that we would specify the size, but that's not necessary. If C++ doesn’t check to be sure that your array references are in bounds, then it doesn’t need to know (in the function) how big it is. It only needs the size when the argument array is declared (in order to know how much memory to set aside)
上海交通大学交大密西根 ·联合学院一 181 UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University Array in Function Declaration C++will in fact allow you to specify the size of the array in the parameter declaration,but will ignore it. Passing Arrays as Parameters We can specify the size,but it is ignored void ClearIntegerArray(int array [N VALUES], int n);
Array in Function Declaration Array in Function Declaration • C++ will in fact allow you to specify the size of the array in the parameter declaration, but will ignore it