c语言结构体数组初始化


最近看一段代码有所迷惑,先简单总结一下。有关结构体数组初始化的问题

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(u8path,u8 mode)", 

(void)mf_close,"u8 mf_close(void)", 

(void
)mf_read,"u8 mf_read(u16 len)", 

(void)mf_write,"u8 mf_write(u8dat,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(u8pname)", 

(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(u8c)",  


};

_m_usmart_nametab的结构体如下

 //函数名列表  
struct _m_usmart_nametab
{
void* func; //函数指针
const u8* name; //函数名(查找串)  
};
一开始一直不明白,为什么下边的只有两个参数,而上边的确有那么多,难道不是一一对应?

其实上边的是一个table表,就像 u8 table[]={};

而上边的定义的是一个结构体的数组,其成员很多。但每一个成员应该和结构体类型一致,这只不是定义一个这样结构类型的结构体数组而已。

看下面例子


typedef struct _TEST_T {
int i;
char c[10];
}TEST_T;
TEST_T gst = {1, “12345”};//可以初始化,设置i为1,s为一个字符串.

TEST_T gst = {1};//初始化个数少于实际个数时,只初始化前面的成员。

TEST_Tgst = {.c=“12345”};//有选择的初始化成员。

复合字面量。

gst = (TEST_T){122, "1256"};//这是一个赋值语句,也可以作为初始化。可以出现在程序的任何地方。


当然也可以使用复合字面量来初始化:

gst = (TEST_T){.i=122, .c="123"};


3、结构体数组

可以用多个大括号括起来:

TEST_T gst[10] = {{},{},{},{}}

也可以初始化其中的一个元素:

TEST_T gst[10] = {[2]={}, [3]={}}

也可以使用复合字面量:

TEST_T gst[10] = {[2].i=0, [3].i={}}


sitemap