【问题标题】:const, pointers, typedefs and stars [duplicate]常量、指针、类型定义和星号 [重复]
【发布时间】:2011-08-01 16:07:05
【问题描述】:

可能重复:
what is the difference between 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;

}

【问题讨论】:

标签: c++ pointers constants typedef


【解决方案1】:

我不了解,但会尝试回答。

一个类型不需要多个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就很容易理解了。希望对您有所帮助!

【讨论】:

  • 嗨 Mahesh 如果我的理解正确 typedef const int* PointerToConstInt1; typedef const int PointerToConstInt2; typedef int const PointerToConstInt3; typedef int const *PointerToConstInt4;应该都代表相同的......
  • +1:这几乎是正确的总结。
  • typedef int const PointerToConstInt3; typedef const int PointerToConstInt2; 表示相同但它们不是指针。它们只是常量整数的 typedef。 const int PI = 3.147;int const PI = 3.147;
  • @RandomCPlusPlus - 我认为您在代码中完美地分组了不同的语义,并带有您的评论。 @Mahesh - 仔细检查你的答案:const int const* 不应该是指向常量值的常量指针,而是指向常量值的指针,它恰好被限定了两次。
  • @RandomCPlusPlus - 正确。 typedef const int *consttypedef int const *const 相同,都是指向常量值的常量指针。我认为标准定义const 限定其左侧的任何内容。然而,对于典型的const type blah 表达式,大多数编译器会认为它与type const blah 相同。为简单起见,只需抓取编译器读取的任何内容,然后从右向左读取即可。
【解决方案2】:

有关使用const 的一般规则,StackOverflow 上有数十亿个问题。例如:const usage with pointers in C

关于typedef,typedef的写法与变量声明的写法相同;唯一的区别是你在它前面加上typedef,然后用typedef的名字替换变量的名字! (除了少数例外,通常用括号固定)。所以,例如:

char *flaps;

变成:

typedef char *flaps_type;

【讨论】:

  • 这似乎很有用 - 让我考虑一下
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-02-23
  • 1970-01-01
  • 1970-01-01
  • 2012-04-22
  • 2015-04-26
  • 1970-01-01
相关资源
最近更新 更多