主源文件中的结构值未更新
Structure value is not updating in main source file
我项目的一部分,一些源文件是
button_key.h, button_key.h, lcd.h, mani.c
等
使用了 button_key.H 中的结构并声明为
struct menu {
uint8_t Hour;
uint8_t Minute;
uint8_t Second;
};
在main.c
源文件中
#include "lcd.h"
#include "delay.h"
#include "button_key.h"
struct menu s1= {0};
struct menu *ptr;
int main(void)
{ int a;
ptr = &s1;
//some code//
menu_key_display (s1,menu_display);
LCD_DisplayNumber(10,(*ptr).Hour,2); // here not updating the structure value as per the code in button_key.c only show zero (0)
while(1);
// tried also LCD_DisplayNumber(10,s1.Hour,2); also seems same reult.
}
结构在button_key.c
文件中使用(仅部分代码)
void menu_key_display(struct menu s1,const char *menu_display[])
{ //some cdoe here
case 1: // set time
{
LCD_Clear();
LCD_GoToLine(0);
LCD_DisplayString(menu_display[5]);
LCD_GoToLine(1);
LCD_DisplayString(" HH:MM:SS:PM/AM");
UP_Down_Keyvalue(s1,2,4);
break;
// some code
}
以上源代码对菜单结构成员的值进行了更改。但是这些变化并没有反映在 main.c 我怎么了。
根据评论编译答案:
缺少更新仅在您在代码中注释的行中可见,即它们只是症状。
问题的原因早了一行:
menu_key_display (s1,menu_display);
// ^
为了让函数内部的变化在外部可见,
你需要使用 "call by reference",这在 C 中意味着 "via a pointer"。
因此,将有问题的行更改为:
menu_key_display (ptr,menu_display);
这当然必须通过更改被调用函数的行来匹配:
void menu_key_display(struct menu *s1Ptr, const char *menu_display[])
// ...
UP_Down_Keyvalue(s1Ptr,2,4); // assuming this to be supposed to have the missing effect
// ^
指针的最后一次使用(与 "call by value" 结构复制相反)也需要以类似的方式反映在 UP_DownKeyvalue(...)
函数中。
但是,如果它是一个宏(我想我在我的答案的前一个版本中隐含地假定了这一点),那么 UP_Down_Keyvalue(*s1Ptr,2,4);
可以在不改变宏定义的情况下使用。
请注意,在使用指针时,您可能需要向函数添加检查,以确保指针不为 NULL。称之为偏执狂,但偏执狂对于程序员(至少是嵌入式程序员)来说是一种健康的心态。
我项目的一部分,一些源文件是
button_key.h, button_key.h, lcd.h, mani.c
等
使用了 button_key.H 中的结构并声明为
struct menu {
uint8_t Hour;
uint8_t Minute;
uint8_t Second;
};
在main.c
源文件中
#include "lcd.h"
#include "delay.h"
#include "button_key.h"
struct menu s1= {0};
struct menu *ptr;
int main(void)
{ int a;
ptr = &s1;
//some code//
menu_key_display (s1,menu_display);
LCD_DisplayNumber(10,(*ptr).Hour,2); // here not updating the structure value as per the code in button_key.c only show zero (0)
while(1);
// tried also LCD_DisplayNumber(10,s1.Hour,2); also seems same reult.
}
结构在button_key.c
文件中使用(仅部分代码)
void menu_key_display(struct menu s1,const char *menu_display[])
{ //some cdoe here
case 1: // set time
{
LCD_Clear();
LCD_GoToLine(0);
LCD_DisplayString(menu_display[5]);
LCD_GoToLine(1);
LCD_DisplayString(" HH:MM:SS:PM/AM");
UP_Down_Keyvalue(s1,2,4);
break;
// some code
}
以上源代码对菜单结构成员的值进行了更改。但是这些变化并没有反映在 main.c 我怎么了。
根据评论编译答案:
缺少更新仅在您在代码中注释的行中可见,即它们只是症状。
问题的原因早了一行:
menu_key_display (s1,menu_display);
// ^
为了让函数内部的变化在外部可见,
你需要使用 "call by reference",这在 C 中意味着 "via a pointer"。
因此,将有问题的行更改为:
menu_key_display (ptr,menu_display);
这当然必须通过更改被调用函数的行来匹配:
void menu_key_display(struct menu *s1Ptr, const char *menu_display[])
// ...
UP_Down_Keyvalue(s1Ptr,2,4); // assuming this to be supposed to have the missing effect
// ^
指针的最后一次使用(与 "call by value" 结构复制相反)也需要以类似的方式反映在 UP_DownKeyvalue(...)
函数中。
但是,如果它是一个宏(我想我在我的答案的前一个版本中隐含地假定了这一点),那么 UP_Down_Keyvalue(*s1Ptr,2,4);
可以在不改变宏定义的情况下使用。
请注意,在使用指针时,您可能需要向函数添加检查,以确保指针不为 NULL。称之为偏执狂,但偏执狂对于程序员(至少是嵌入式程序员)来说是一种健康的心态。