fork download
  1. #include <iostream>
  2. using namespace std;
  3. // 第一個函式,帶有 const 陣列參數
  4. void processArray(const int arr[], int size);
  5.  
  6. // 第二個函式,正確地使用 const 接收陣列
  7. void anotherFunction(const int arr[], int size) {
  8. processArray(arr, size); // 正確,因為 processArray 也使用 const
  9. }
  10.  
  11. // 函式定義
  12. void processArray(const int arr[], int size) {
  13. for (int i = 0; i < size; ++i) {
  14. cout << arr[i] << " ";
  15. }
  16. cout << endl;
  17. }
  18.  
  19. int main() {
  20. int numbers[] = {5, 10, 15, 20};
  21. anotherFunction(numbers, 4);
  22. return 0;
  23. }
  24.  
  25.  
  26.  
Success #stdin #stdout 0s 5280KB
stdin
8
stdout
5 10 15 20