[英]How to fill a structure defined in one .c file in a function defined in another .c without passing the pointer as an argument?
I have a C function (store_mode_ - defined in file_A.c) which is called in a Fortran code.我有一个在 Fortran 代码中调用的 C 函数(store_mode_ - 在 file_A.c 中定义)。
It looks like this..看起来像这样..
// Create a global structure to populate with current mode frequencies
struct minos_modes current_modes;
static int nmode = 0;
void
store_mode_( int *n, int *l, double *w, double *U )
{
if ( nmode == MODE_MAX )
{
printf( "[ store_mode_ ] Error: MODE_MAX is too small\n" );
exit( 1 );
}
current_modes.mode_n[nmode] = *n;
current_modes.mode_l[nmode] = *l;
current_modes.mode_w[nmode] = *w;
current_modes.mode_U[nmode] = *U;
nmode += 1;
current_modes.len = nmode;
}
I am trying to fill a structure with the values (n, l, w, U) passed as arguments in the Fortran call.我试图用在 Fortran 调用中作为参数传递的值(n、l、w、U)填充结构。 The above code works when defined immediately above my main in File_B.c, which calls the fortran code within it.
上面的代码在 File_B.c 中定义我的 main 之上时起作用,它调用其中的 fortran 代码。
However.. If I define the above function in file_A.c (the one that main isn't in) it doesn't work.但是.. 如果我在 file_A.c 中定义了上面的函数(main 不在的那个)它不起作用。 I appreciate this is due to the lack of shared globals between .c files.
我很欣赏这是由于 .c 文件之间缺少共享全局变量。 I was wondering if there is a solution for this?
我想知道是否有解决方案? Perhaps with extern, though I played a little with no success.
也许用extern,虽然我玩了一点但没有成功。
static int nmode = 0;
If you are including that line in both files, then you have two nmode
variables, one in each file.如果您在两个文件中都包含该行,则您有两个
nmode
变量,每个文件中一个。 The static
makes the symbol private to the translation unit . static
使符号对翻译单元私有。
Instead, you probably want相反,你可能想要
int nmode;
in one file, and在一个文件中,以及
extern int nmode;
in the other.在另一个。 (There's no need to initialize it to zero explicitly. That's guaranteed for static storage.)
(无需显式将其初始化为零。静态存储可以保证这一点。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.