gpt4 book ai didi

Const Struct 与 Const 成员的结构

转载 作者:行者123 更新时间:2023-12-02 16:44:09 25 4
gpt4 key购买 nike

这两者有区别吗?

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 结构(底部)是否不同?

最佳答案

主要区别在于意图之一。

typedef struct {
const birthday_t birthday;
const unsigned int id;
}person_t;

说没有 person_t 可以改变它的 birthdayid

const person_t person = {
.birthday = {1,20,2000},
.id = 123};

(假设 person_t 的第二个声明)说这个特定的人不能改变它的 birthdayid,但其他人可能反对。

关于Const Struct 与 Const 成员的结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60911748/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com