这两者之间有区别吗?
typedef struct {
unsigned int day;
unsigned int month;
unsigned int year;
}birthday_t;
typedef struct {
const birthday_t birthday;
const unsigned int id;
}person_t;
person_t person = {
.birthday = {1,20,2000},
.id = 123};
和
typedef struct {
unsigned int day;
unsigned int month;
unsigned int year;
}birthday_t;
typedef struct {
birthday_t birthday;
unsigned int id;
}person_t;
const person_t person = {
.birthday = {1,20,2000},
.id = 123};
如果结构内部的成员是const但结构不是常数(顶部),这与没有const成员的const结构(底部)不同吗?
主要区别是意图之一。
typedef struct {
const birthday_t birthday;
const unsigned int id;
}person_t;
说没有person_t
可以更改其birthday
或id
。
const person_t person = {
.birthday = {1,20,2000},
.id = 123};
((假设person_t
的第二种形式表示此特定人员无法更改其birthday
或id
,但其他人可以更改。