const struct
是什么意思?它与struct
不同吗?'const struct'与'struct'有什么不同?
30
A
回答
57
const
部分确实适用于变量,而不是结构本身。
例如@Andreas正确说:
const struct {
int x;
int y;
} foo = {10, 20};
foo.x = 5; //Error
但重要的是,可变foo
是恒定的,而不是struct
定义本身。 你同样可以写为:
struct apoint {
int x;
int y;
};
const struct apoint foo = {10, 20};
foo.x = 5; // Error
struct apoint bar = {10, 20};
bar.x = 5; // Okay
17
这意味着struct
是恒定的,即在初始化后您不能编辑它的字段。
const struct {
int x;
int y;
} foo = {10, 20};
foo.x = 5; //Error
编辑:格雷厄姆正确地指出的常量性是可变的属性,在这种情况下foo
,而不是结构定义:
struct Foo {
int x;
int y;
};
const struct Foo foo = {10, 20};
foo.x = 5; //Error
struct Foo baz = {10, 20};
baz.x = 5; //Ok
-1
我认为,一个const结构不能被修改。换句话说,被声明为const的struct的所有字段都是不可修改的。
+4
这到底是什么意思是“我相信”?如果你不合格,不要回答。留下猜测的评论,不是吗? – nbro 2016-03-19 15:42:50
0
常量意味着你不能编辑的声明和初始化后的结构领域,你可以检索数据形成结构
0
“常量”作为每个词不断本身就说明是指不可修改。这可以应用于任何数据类型的变量。 struct是用户定义的数据类型,它也适用于任何结构的变量。初始化后,const变量的值不能被修改。
-1
你不能修改常量结构,第一个结构是一个简单的数据类型,因此,当一个const关键字来时,编译器会举行一个寄存器的存储空间,而比临时存储(比如RAM)和存储在寄存器上的变量标识符不可修改
相关问题
- 1. typedef const struct * CGPath和typedef struct * CGPath有什么区别?
- 2. Const Struct&
- 3. const C Struct数组struct struct数组
- 4. “struct a a1 = {0};”不同于“struct a a2 = {5};”为什么?
- 5. struct struct
- 6. 什么是“struct file_operations”参数?
- 7. `typedef struct X {}`和`typedef struct {} X`有什么区别?
- 8. 为什么struct的sizeof与其成员之和不同?
- 9. struct和:: - 它做什么
- 10. 为什么静态const char *模板struct成员未初始化
- 11. 不同类型的struct
- 12. c - struct - 额外的“:1”在做什么?
- 13. “struct node * temp”和“struct node *&temp”之间的区别是什么?
- 14. 为什么我不能在C#中的struct中具有相同的struct type属性?
- 15. Struct不接受wchar_t
- 16. 使用const void *((struct str_name *)p) - > str_dataitem;
- 17. 期望'struct matrix_t *',但参数的类型是'struct matrix_t *'?_?没有什么区别
- 18. “struct struct”中的限制
- 19. STRUCT与指向函数
- 20. 为什么文件中写入结构的endian与内存中的struct不同?
- 21. bool operator == typedef struct
- 22. Boost multiindex complex struct
- 23. 将'sqlite3 * const *'(又名'struct sqlite3 * const *')传递给'sqlite3 **'类型的参数(又名'struct sqlite3 **')丢弃限定符
- 24. struct kevent中的udata字段是什么?
- 25. 什么是win32上的“struct EVENT_LIST”?
- 26. C++:typedef struct {......} * node是什么意思?
- 27. ws2def.h中的struct sockadddr_dl用于什么?
- 28. 为什么struct字段显示为空?
- 29. 为什么sizeof在ctypes struct packing的给定示例中有所不同?
- 30. 在struct []
干杯。我能为我的观察获得+1吗? :D – GrahamS 2010-11-24 13:29:58
当然,在这里你去;) – 2010-11-24 13:39:52