免费电子图书下载,哪种石斛最好,美人泪山河恋
1 在块中使用static修饰变量
#include <stdio.h> void trystat(void); int main(void) { int count; for (count = 1; count <= 3; count++) { printf("here comes iteration %d:\n", count); trystat(); } return 0; } void trystat(void) { int fade = 1; static int stay = 1; printf("fade = %d and stay = %d\n", fade++, stay++); }
程序执行结果:
here comes iteration 1:
fade = 1 and stay = 1
here comes iteration 2:
fade = 1 and stay = 2
here comes iteration 3:
fade = 1 and stay = 3
(1) 这里变量stay
,它从程序加载时开始存在,直到程序终止。但是它的范围仅限于trystat()
函数块。只有当这个函数执行时,程序才能使用stay
访问它指定的对象.
(2) 变量stay
记得它的值增加了1,但是变量fade每次都会重新开始。这指出了初始化的不同之处:fade
在trystat()
每次调用都从新初始化,而stay
变量只初始化一次。
(3) 静态变量在程序加载到内存之后就已经就位了。将语句static int stay = 1;
放在trystat()函数中告诉编译器,只允许trystat()
函数查看变量;它不是在运行时执行的语句。
2 在任何函数外部使用static修饰变量
static bool wavtaskcreated = false; static queuehandle_t wav_msg_queue = null; static wav_play_queue_t wavplayqueue; static bool wav_get_version_flag = false; static char wav_version[32]; static uint8_t *wav_getfilename(wav_type_t wav_type) { } static bool wav_getfileinfo(wav_file_info_t *pfileinfo, uint8_t *pfilename) { } static bool wav_getversion_internal(wav_file_info_t *pfileinfo) { } static bool wav_readfile(uint8_t *pdata, uint32_t offset, uint32_t size) { } static bool wav_sendtoda(uint8_t *pfile, uint32_t size, uint32_t volume) { } static bool wav_put_queue(wav_type_t wav_type, bool fromisr) { } static bool wav_get_queue(wav_type_t *pwavtype) { } static bool wav_play_inernal(wav_type_t wav_type) { } void wav_init(void) { da_init(16000); if (!wavtaskcreated) { wavtaskcreated = true; xtaskcreate(wav_task, "wav", stack_size_task_wav, null, priority_task_wav, null); wav_msg_queue = xqueuecreate(wav_msg_queue_length, sizeof(wav_message_t)); wavplayqueue.pwavtypetable = malloc(wav_paly_queue_max * sizeof(wav_type_t)); } wavplayqueue.front = 0; wavplayqueue.end = 0; wavplayqueue.sizenow = 0; wavplayqueue.totalsize = wav_paly_queue_max; } bool wav_play(wav_type_t wav_type, bool force) { if (!wavtaskcreated) { return false; } if (force) { vportentercritical(); watchdog_feed(); wav_play_inernal(wav_type); vportexitcritical(); return true; } bool rv = wav_put_queue(wav_type, false); vtaskdelay(30); return rv; }
上述为某平台使用da播放wav的程序片段,
static uint8_t *wav_getfilename(wav_type_t wav_type); static bool wav_getfileinfo(wav_file_info_t *pfileinfo, uint8_t *pfilename); static bool wav_getversion_internal(wav_file_info_t *pfileinfo); static bool wav_readfile(uint8_t *pdata, uint32_t offset, uint32_t size); static bool wav_sendtoda(uint8_t *pfile, uint32_t size, uint32_t volume); static bool wav_put_queue(wav_type_t wav_type, bool fromisr); static bool wav_get_queue(wav_type_t *pwavtype); static bool wav_play_inernal(wav_type_t wav_type);
上述函数为内部函数使用static
修饰。
void wav_init(void); bool wav_play(wav_type_t wav_type, bool force);
上述两个函数为模块接口(初始化da和播放wav功能)供外部调用,没有使用static修饰。
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
如何在没有core文件的情况下用dmesg+addr2line定位段错误
用QT制作3D点云显示器——QtDataVisualization
网友评论