const、指针、typedef和星形

const, pointers, typedefs and stars

本文关键字:typedef 指针 const      更新时间:2023-10-16

可能重复:
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就很容易理解了。希望它能有所帮助!