gpt4 book ai didi

c++ - 在此代码中使用 const

转载 作者:太空宇宙 更新时间:2023-11-04 15:03:06 24 4
gpt4 key购买 nike

<分区>

此代码在不使用 const 的情况下编译和执行良好。在这里使用 const 是否有特殊原因?

0   #include <iostream>
1 using namespace std;
2
3 int sum(const int array[], const int length) {
4 long sum = 0;
5 for(int i = 0; i < length; sum += array[i++]);
6 return sum;
7 }
8
9 int main() {
10 int arr[] = {1, 2, 3, 4, 5, 6, 7};
11 cout << "Sum: " << sum(arr, 7) << endl;
12 return 0;
13 }

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com