[英]How can I fill a structure with the values of an identical structure within a function?
Okay so here's my problem: 好的,这是我的问题:
1) I have a structure defined say like this: 1)我有一个这样定义的结构:
struct minos_model
{
int ifanis, ifdeck, npts, icb, cmb, noc;
double tref;
double r[MODEL_NPTS_MAX], rho[MODEL_NPTS_MAX], vpv[MODEL_NPTS_MAX], vsv[MODEL_NPTS_MAX],
qk[MODEL_NPTS_MAX], qmu[MODEL_NPTS_MAX], vph[MODEL_NPTS_MAX], vsh[MODEL_NPTS_MAX], eta[MODEL_NPTS_MAX];
char model_name[MODEL_LINE_LEN];
};
typedef struct minos_model model_t;
2) I have declared some structures of this form: 2)我已经声明了这种形式的一些结构:
/* Declare structures */
model_t candidate_mod, current_mod, empty_mod;
3) I then pass them into a function declared as something like this: 3)然后,将它们传递给声明为类似以下内容的函数:
void
perturb_model( model_t *candidate_mod, model_t *current_mod, model_t *empty_mod )
{
candidate_mod = current_mod; // *THIS LINE*
<various other functions and whatnot>
}
4) I then call the function like so: 4)然后我像这样调用函数:
perturb_model( &candidate_mod, ¤t_mod, &empty_mod );
// arguments passed as pointers
So the above is a simplified version of what I've done. 因此,以上是我所做工作的简化版本。 I am trying (on line marked THIS LINE in step 3) to set candidate_mod to a different structure (current_mod), I would use a loop but one, this seems inelegant, two, sometimes the structure I am setting it equal too (current_mod) contains fewer values (than candidate_mod) and as such I am worried the excess values from the original structure (candidate_mod) would remain.
我正在尝试(在第3步中标记为THIS LINE的行上)将候选者_mod设置为不同的结构(current_mod),我会使用一个循环,但是其中一个看起来不太优雅,两个,有时我也将其设置为相等的结构(current_mod)包含的值(比候选者_mod少),因此,我担心原始结构(candidate_mod)中的多余值会保留。
I should also mention, since perhaps it's relevant, that the function is defined in a separate c file linked in the makefile, so perhaps there is a difficulty with sharing globals.. but I think passing the pointer as an argument should solve this? 我还应该提到,因为可能是相关的,该函数是在makefile中链接的单独的c文件中定义的,所以共享全局变量可能会有困难..但是我认为将指针作为参数传递应该可以解决此问题?
Also, it seems from my research that perhaps the use of extern might help, though I cannot figure out how after many attempts. 同样,从我的研究看来,也许使用extern可能会有所帮助,尽管我无法弄清楚经过多次尝试后该如何做。
If someone could explain the best way to do this I would be extremely grateful!! 如果有人能够解释做到这一点的最佳方法,我将万分感谢! I know how to do it trivially in my main code but the structures have to be assigned like this within the function.
我知道如何在我的主代码中轻松完成此操作,但是必须在函数中像这样分配结构。
Cheers. 干杯。
只是将src的值放入dest中,不要分配指针...但是您的真正需求并不清楚。
*candidate_mod = *current_mod;
You are passing an address of the structure. 您正在传递结构的地址。 When you say
candidate_mod = current_mod;
当你说
candidate_mod = current_mod;
you are assigning the address of current_mod
to candidate_mod
which is not correct. 您正在将
current_mod
的地址分配给不正确的candidate_mod
。 You have to dereference that structure using code like this 您必须使用如下代码取消引用该结构
*candidate_mod = *current_mod;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.