主源文件中的结构值没有更新 [英] 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
}
上述源代码做了更改,值为菜单结构成员。但这些变化并没有从根本上反映出来。c我有什么问题吗?
推荐答案
根据评论编译答案:
缺少更新仅在您在代码中注释的行中可见,即它们只是症状。
问题的原因在前一行:
menu_key_display (s1,menu_display);
// ^
为了使函数内部的更改在外部可见,
您需要使用"按引用调用",这在C中的意思是"通过指针"。
因此,将有问题的行更改为:
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);
而不更改宏定义。
这篇关于主源文件中的结构值没有更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文