const、指针、typedef和星形
const, pointers, typedefs and stars
可能重复:
const int*,const int*const,int const*之间有什么区别
大家好,
我想知道当指针涉及以下内容时,是否有人能澄清typedef的语法:-恒星位置的含义;-const位置的意义;-二者之间的互动。
在我写下下面的例子后,我对const和star发生了什么有了一个想法,但我只是通过反复尝试得到了代码,我希望有知识渊博的人能给出更理论的解释。
感谢
#include <iostream>
using namespace std;
int main() {
int paolo = 10;
// POINTERS
typedef int* PointerToInt1;
typedef int *PointerToInt2;
// POINTERS TO CONST
typedef const int* PointerToConstInt1;
typedef const int *PointerToConstInt2;
typedef int const* PointerToConstInt3;
typedef int const *PointerToConstInt4;
// qualifying with const twice
// ignored - simply gets you a warning
typedef const int const* PointerToConstInt5;
typedef const int const *PointerToConstInt6;
// CONST POINTERS
typedef int *const ConstPointerInt1;
typedef int* const ConstPointerInt2;
// CONST POINTERS TO CONST
typedef const int *const ConstPointerToConstInt1;
// POINTERS
int *ip1 = &paolo;
int* ip2 = &paolo;
PointerToInt1 ip3 = &paolo;
PointerToInt2 ip4 = &paolo;
// POINTERS TO CONST
PointerToConstInt1 ip11;
PointerToConstInt2 ip12 = &paolo;
PointerToConstInt3 ip13;
PointerToConstInt4 ip14 = &paolo;
PointerToConstInt3 ip15;
PointerToConstInt4 ip16 = &paolo;
/*
// POINTERS TO CONST
//ALL ERROR
*ip11 = 21; *ip12 = 23;
*ip13 = 23; *ip14 = 23;
*ip15 = 25; *ip16 = 23;
*/
// CONST POINTERS
// ERROR - No initialiser
// ConstPointerInt1 ip21;
// ConstPointerInt2 ip22;
int me = 56;
int you = 56;
ConstPointerInt1 ip21 = &me;
ConstPointerInt1 ip22 = &me;
*ip21 = 145;
*ip22 = 145;
// ERROR - No initialiser
// ConstPointerToConstInt1 ip31;
// ERROR - Cant change eobjected pointed at
ConstPointerToConstInt1 ip31 = &me;
// ip31 = &you;
// ERROR - Cant change the value of objected pointed at
ConstPointerToConstInt1 ip33 = &me;
// *ip31 = 54;
cout << *ip1 << *ip2 << *ip4 << endl;
cout << *ip11 << *ip12 << *ip13 << *ip14 << *ip15 << *ip16 << endl;
return 1;
}
关于使用const
的一般规则,这里有关于StackOverflow的数十亿个问题。例如:C.中指针的常量用法
关于typedef,编写typedef的规则与编写变量声明的规则相同;唯一的区别是用typedef
作为前缀,并用typedef的名称替换变量的名称!(除了少数例外,这些例外通常用括号固定(。例如:
char *flaps;
变为:
typedef char *flaps_type;
我知识不多,但会尝试回答。
一个类型不需要多个typedef
语句。
// POINTERS TO CONST
typedef const int* PointerToConstInt1;
typedef const int *PointerToConstInt2; // Above two represent the same
typedef int const* PointerToConstInt3;
typedef int const *PointerToConstInt4; // 3 & 4 represent the same
空间在这里并不重要。
int* ptr ;
int *ptr ; // Both mean the same.
const int*
表示指针可以指向不同的位置,但它所指向的位置中的值不能更改。
int* const
表示常量指针。它不能指向其他位置,但可以更改它所指向位置的值。由于它是一个常量指针,因此必须在声明时初始化它。
const int* const
表示指向常数值的常指针。它既不能指向不同的位置,也不能更改它所指向位置的值。因此,在声明时必须初始化它。
ConstPointerToConstInt1 ip31; // Error : No initialization
ConstPointerToConstInt1 ip31 = &me;
ip31 = &you; // Error: Trying to modify the pointer to point to a different location
ConstPointerToConstInt1 ip33 = &me;
*ip31 = 54; // Error: Trying to change the value it is pointing at.
如果你理解以上错误,const int*
和int* const
就很容易理解了。希望它能有所帮助!
相关文章:
- 如何将成员函数作为回调参数传递给需要"typedef-ed"自由函数指针的函数?
- 向前声明指向类的 typedef 指针
- 使用智能指针指向 C 库中的结构,该结构通过 typedef 隐藏实现(即不完整的类型)
- 对于指向C++类的不透明C指针,正确的typedef是什么
- "typedef"类型名称是否可以像"struct"定义那样声明指向结构的指针?
- C++ typedef 函数指针,并在一个语句中声明一个指针
- 指向具有 2 个参数的 WINAPI 函数的 Typedef 指针
- 返回没有 typedef 的成员函数指针的语法是什么?
- Typedef 函数与函数指针
- 调用指针时违反 C++ Typedef 访问冲突
- 将指向前向声明类型的指针推送到 typedef'd 向量时出现编译器错误
- 将typedef方法作为指针函数传递
- 是无限的Typedef函数指针继承
- 带有功能指针的Typedef:函数不存在
- 如何将方法指针声明为Typedef方法参数
- 函数指针typedef的语法是什么
- 如何用模板参数对函数指针进行typedef
- 使用 vs typedef 指针到noexcept-function不一致
- 推导 typedef'd 函数指针的模板参数
- "using" (typedef) 指针:它的默认构造函数返回 nullptr?