我有一个数组 c[7][8](或 c[56])。我必须将数组的 8 个元素传递给一个函数(在不同的类中)7 次。从这个函数,我再次必须将它传递给另一个函数。我试过的是
int main(){
...
double a[]={2.1,2.2,2.3,....};//7 elements
double b[]={1.1,1.2,1.3,....};//8 elements
double c[]={0.5,0.0,0.4,....};//56 elements. I actually want to use c[7][8]; but I thought c[56] would be easier
for (int i=0; i<7; i++){
classa.calc(a[i],b[i],&c[i*8]); //assuming I use the 1D array for c. I don't want to pass the array a and b, but ith element.
//for c, I want to pass 8 consecutive elements of c each time i call the function like c[0-6],c[7-13] etc
}
}
a 和 b 是两个不同的数组,我必须在函数中使用 element(i) 。
现在,在课堂上:
class classa{
void function(double* c, double* r) {
...
for (int i=0; i<8; i++) c[i]=h*c[i]*pow(x,i));//here an algorithm is used to get the new value of c as an existing function of c. the given function is just a part of the algorithm.
for (int j=0; j<N1; j++) r[j]=some function of c;
}
public:
//here I want c to be used as a 1D array of 8 elements. same in function too
...
void calc(double a, double b, double* c){
function(&c[0]);
...
}
};
当我运行程序时,我只得到前 8 个元素的结果,并且给出了分段错误。我如何解决它?