c語言結(jié)構(gòu)體數(shù)組初始化
struct _m_usmart_nametab usmart_nametab[]=
{#if USMART_USE_WRFUNS==1 //如果使能了讀寫操作(void*)read_addr,"u32 read_addr(u32 addr)",(void*)write_addr,"void write_addr(u32 addr,u32 val)", #endif(void*)delay_ms,"void delay_ms(u16 nms)",(void*)delay_us,"void delay_us(u32 nus)", (void*)LCD_Clear,"void LCD_Clear(u16 Color)",(void*)LCD_Fill,"void LCD_Fill(u16 xsta,u16 ysta,u16 xend,u16 yend,u16 color)",(void*)LCD_DrawLine,"void LCD_DrawLine(u16 x1, u16 y1, u16 x2, u16 y2)",(void*)LCD_DrawRectangle,"void LCD_DrawRectangle(u16 x1, u16 y1, u16 x2, u16 y2)",(void*)Draw_Circle,"void Draw_Circle(u16 x0,u16 y0,u8 r)",(void*)LCD_ShowNum,"void LCD_ShowNum(u16 x,u16 y,u32 num,u8 len,u8 size)",(void*)LCD_ShowString,"void LCD_ShowString(u16 x,u16 y,u16 width,u16 height,u8 size,u8 *p)", (void*)LCD_ReadPoint,"u16 LCD_ReadPoint(u16 x,u16 y)",(void*)fsmc_sram_test_write,"void fsmc_sram_test_write(u8 data,u32 addr)",(void*)fsmc_sram_test_read,"u8 fsmc_sram_test_read(u32 addr)",(void*)mymalloc,"void *mymalloc(u8 memx,u32 size)",(void*)myfree,"void myfree(u8 memx,void *ptr)",(void*)mf_mount,"u8 mf_mount(u8 drv)",(void*)mf_open,"u8 mf_open(u8*path,u8 mode)",(void*)mf_close,"u8 mf_close(void)",(void*)mf_read,"u8 mf_read(u16 len)",(void*)mf_write,"u8 mf_write(u8*dat,u16 len)",(void*)mf_opendir,"u8 mf_opendir(u8* path)",(void*)mf_readdir,"u8 mf_readdir(void)",(void*)mf_scan_files,"u8 mf_scan_files(u8 * path)",(void*)mf_showfree,"u32 mf_showfree(u8 *drv)",(void*)mf_lseek,"u8 mf_lseek(u32 offset)",(void*)mf_tell,"u32 mf_tell(void)",(void*)mf_size,"u32 mf_size(void)",(void*)mf_mkdir,"u8 mf_mkdir(u8*pname)",(void*)mf_fmkfs,"u8 mf_fmkfs(u8 drv,u8 mode,u16 au)",(void*)mf_unlink,"u8 mf_unlink(u8 *pname)",(void*)mf_rename,"u8 mf_rename(u8 *oldname,u8* newname)",(void*)mf_gets,"void mf_gets(u16 size)",(void*)mf_putc,"u8 mf_putc(u8 c)",(void*)mf_puts,"u8 mf_puts(u8*c)",};
_m_usmart_nametab的結(jié)構(gòu)體如下
//函數(shù)名列表 struct _m_usmart_nametab{void* func; //函數(shù)指針const u8* name; //函數(shù)名(查找串) };一開始一直不明白,為什么下邊的只有兩個參數(shù),而上邊的確有那么多,難道不是一一對應?
其實上邊的是一個table表,就像 u8 table[]={};
而上邊的定義的是一個結(jié)構(gòu)體的數(shù)組,其成員很多。但每一個成員應該和結(jié)構(gòu)體類型一致,這只不是定義一個這樣結(jié)構(gòu)類型的結(jié)構(gòu)體數(shù)組而已。
看下面例子
typedef struct _TEST_T {int i;char c[10];}TEST_T;TEST_T gst = {1, “12345”};//可以初始化,設置i為1,s為一個字符串.
TEST_T gst = {1};//初始化個數(shù)少于實際個數(shù)時,只初始化前面的成員。
TEST_Tgst = {.c=“12345”};//有選擇的初始化成員。
復合字面量。
gst = (TEST_T){122, "1256"};//這是一個賦值語句,也可以作為初始化??梢猿霈F(xiàn)在程序的任何地方。
當然也可以使用復合字面量來初始化:
gst = (TEST_T){.i=122, .c="123"};
3、結(jié)構(gòu)體數(shù)組
可以用多個大括號括起來:
TEST_T gst[10] = {{},{},{},{}}
也可以初始化其中的一個元素:
TEST_T gst[10] = {[2]={}, [3]={}}
也可以使用復合字面量:
TEST_T gst[10] = {[2].i=0, [3].i={}}