const, pointers, typedefs and stars
本问题已经有最佳答案,请猛点这里访问。
Possible Duplicate:
what is the difference between const int*, const int * const, int const *
大家好,
我想知道,当指针涉及到以下内容时,是否有人可以提供关于typedef语法的一些清晰信息:-恒星位置的含义;-const位置的含义;-二者之间的相互作用。
在我写了下面的例子之后,我对const和star发生了什么有了一个大致的了解,但是我只是通过尝试和错误得到了代码,我想从一个知识渊博的人那里得到一个更理论上的解释。
谢谢
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | #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; } |
(P)我不知道但我会给你一个答案。(p)(P)一种类型的多字母标语不需要多字母标语。(p)字母名称(P)空间并不真的重要在这里。(p)字母名称(P)EDOCX1 3 Diversal means point can point to a different location but the value in the location it is pointing at cannot be changed.(p)(P)字母名称4它无法确定不同的地点,但它可以改变地点中的价值。由于这是一个固定的观点,因此必须首先作出声明。(p)(P)EDOCX1是指一个恒定值的恒定点。这一点可能与地点不同,也可能改变地点中的价值。So,it must be initialized while declaration.(p)字母名称(P)如果你能理解这一可怕的错误,EDOCX1的音标3和EDOCX1的音标4可以轻松理解。希望有帮助!(p)
(P)For general rules on using EDOCX1 pental 0,there are billions of questions here on stackoverflow.For instance:const usage with pointers in C.(p)(P)Regarding typedefs,the rule for writing a typedef is the same for writing a variable declaration;the only difference is that you prefix it with EDOCX1 disciplinary 1,and replace the name of the variable with the name of the typedef!(with a few exceptions,which are typically fixed with parents).So,for instance:(p)字母名称(P)贝卡:(p)字母名称