[英]Passing struct to a function inside a different .c file but it doesn't direct to a right struct
I am trying to pass a struct to a function, but the function I am passing the struct to, is located inside another .c file. 我试图将结构传递给函数,但是将结构传递给的函数位于另一个.c文件中。
When I run debug, the struct's address in the other file is different, thus the variables don't get updated within the receiving function. 当我运行debug时,另一个文件中的结构地址不同,因此变量不会在接收函数中被更新。 The reason I do it this way is because I have various menus in my program and I want to have a separate file for each menu. 之所以这样做,是因为程序中有多个菜单,并且每个菜单都需要一个单独的文件。
inside defs.h file 在defs.h文件中
typedef struct{
byte min;
byte max;
}menuOptionValue_s;
inside menu.h file 在menu.h文件中
void draw_menuOption(menuOptionValue_s);
inside menu.c file 在menu.c文件中
#include "defs.h"
#include "menu.h"
void draw_menuOption(menuOptionValue_s menu){
byte value = menu.max;
draw(x,y,value);
}
inside settings.c file 在settings.c文件中
#include "defs.h"
#include "menu.h"
void setting_Init(void);
menuOptionValue_s setting;
menuOptionValue_s second_setting;
void setting_Init(void){
setting.max = 10;
setting.min = 1;
second_setting.max = 20;
second_setting.min = 11;
draw_menuOption(setting);
draw_menuOption(second_setting);
}
I want to be able to call draw_menuOption() from any .c file and pass structs within those .c files The code I provided is significantly shrank, the setting_Init() is called once in the main.c and draw_menuOption(setting) is called from within a different function inside a setting.c , I just wanted to show that I am actually calling it. 我希望能够从任何.c文件中调用draw_menuOption()并在这些.c文件中传递结构我提供的代码明显缩水,在main.c中一次调用了setting_Init(),并调用了draw_menuOption(setting)从setting.c内部的另一个函数中,我只是想表明我实际上在调用它。
You're passing the object by value . 您正在按值传递对象。 That means a copy is made. 这意味着将进行复制。
Pass a reference or pointer if you want to affect the original object. 如果要影响原始对象,请传递引用或指针。
This topic should be covered early on in your book. 该主题应在您的书中尽早讨论。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.