今天在编写C语言代码时发现了一种结构体赋值的新方法,记录下来提醒自己
首先定义一个结构体
<span style="font-size:14px;">typedef struct _person_struct {
char m_cName[20];
int m_iAge;
char m_cWork[20];
}PERSON_STRUCT;</span>
在主程序中调用:
int main(int argc, char **agrv)
{
PERSON_STRUCT stPerson1 = {
.m_cName = "ZhangSan",
.m_iAge = 15,
.m_cWork = "teacher",
};
printf("stPerson name = %s, age = %d, work = %s\n", stPerson1.m_cName, stPerson1.m_iAge, stPerson1.m_cWork);
return 0;
}
注意下面的赋值方法是错误的,我刚开始就是这么写的
PERSON_STRUCT stPerson1 = {
.m_cName[20] = "ZhangSan",