我将结构作为变量传递给函数,该函数接受结构参数。
然后我正在更改函数内传递的结构的一些值,并且我想更改以相同名称定义的全局变量。
struct Neuron{
double Center[3];
char name;
int xCor;
int yCor;
};
struct Neuron Q;
struct Neuron W;
struct Neuron E;
struct Neuron R;
void Winning_NEURON(struct Neuron K){
for(int i=0; i<3; i++){
K.Center[i] = K.Center[i]+(learningParam*(inputArray[i]-K.Center[i]));
}
因此,如果我将神经元 Q 传递给函数,数据会正确计算,但 Q 神经元值不会更改,我如何才能全局更改而不仅仅是局部更改?
最佳答案
struct Neuron 类型的参数按值传递,这意味着函数接收结构的副本,而原始数据保持不变。要通过引用传递,请将参数设为指针 (struct Neuron *
) 并传递原始地址,例如 &Q
。
关于c - 如何从 C 中的函数更改全局定义的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53992193/