将数组作为参数传递

Pass array as argument

本文关键字:参数传递 数组      更新时间:2023-10-16

我有一个数组:

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

所以问题出在代码的其他地方。您需要向我们提供其余部分。