将数组作为参数传递
Pass array as argument
我有一个数组:
int *BC_type_vel;
BC_type_vel = new int [nBou+1];
和一个功能:
void BC_update (const int type[], float X[]) {
for (int i=1; i<=nBou; ++i) {
if (type[i] == 1) {
std::cout << i << " " << type[i] << " " << BC_type_vel[i] << std:: endl;
for (int e=PSiS[i]; e<PSiE[i]; ++e) {
X[e] = X[elm[e].neigh[0]];
}
}
}
}
我称之为:
BC_update(BC_type_vel,U);
输出为:
1 1 0
2 1 0
3 1 0
4 1 1
5 1 0
那么,为什么函数参数不能正确地复制值呢?
我用gcc尝试了以下代码:
int *BC_type_vel;
int nBou = 10;
void BC_update (const int type[]) {
for (int i=1; i<=nBou; ++i) {
if (type[i] == 1)
std::cout << i << " " << type[i] << " " << BC_type_vel[i] << std:: endl;
}
}
int main () {
int i;
BC_type_vel = new int [nBou+1];
for (i=1; i<=nBou; ++i) {
if (i%2 == 0)
BC_type_vel[i] = i;
else
BC_type_vel[i] = 1;
}
BC_update(BC_type_vel);
return 0;
}
它给出了预期的结果:
1 1 1
3 1 1
5 1 1
7 1 1
9 1 1
所以问题出在代码的其他地方。您需要向我们提供其余部分。
相关文章:
- 在函数中使用 const int size 参数创建数组会在 Visual Studio 中抛出错误 C++:表达式的计
- 如何在模板函数中实例化其长度使用模板参数的数组
- 基于参数创建数组
- 我在解决此错误时遇到问题.我正在努力在主函数中传递数组
- 在 c++ 中使用模板传递数组时出现问题
- Pybind11 默认参数 numpy 数组或 None
- 传递数组结构、ofstream 和 interger 以运行
- 有没有办法根据命令行参数定义数组大小?运行时与编译时实例化?
- 在C++中传递数组作为函数参数的方法
- 函数参数绑定通过参考与传递指针传递数组的规则
- 如果数组变量在 c# 代码中定义为“ref”参数,则如何传递数组变量
- 你究竟如何通过函数的参数传递数组?
- c++ 传递数组而不是可变长度参数列表
- 如何在我的实际参数中通过引用传递数组
- 如何在函数C++中传递数组成员作为参数
- 在函数中传递数组作为引用参数
- 通过引用传递数组参数
- 在c++中传递数组作为函数参数
- 如何在构造函数中传递数组作为参数?c++
- 为什么编译器不在参数中传递数组字符 *arr[] 的大小?