2

Possible Duplicate:
what is the difference between const int*, const int * const, int const *

Hello everyone,

I wonder if somebody can provide some clarity on the syntax for typedefs when pointers are involved in relation to: - meaning of the position of star; - meaning of the position of const; - interaction between teh two.

I get an idea on what's happening with const and star after I wrote the example below but I got that code simply by trial and error and would like to have a more theoretical explanation from someone knowledgeable.

Thanks

#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;

}
Community
  • 1
  • 1
RandomCPlusPlus
  • 359
  • 1
  • 5
  • 12

2 Answers2

3

For general rules on using const, there are billions of questions here on StackOverflow. For instance: const usage with pointers in C.

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 typedef, and replace the name of the variable with the name of the typedef! (With a few exceptions, which are typically fixed with parentheses). So, for instance:

char *flaps;

becomes:

typedef char *flaps_type;
Community
  • 1
  • 1
Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
3

I am not knowledgeable but will give a try to answer.

There is no need of multiple typedef statements for a type.

// 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

Space doesn't really matter here.

int* ptr ;
int *ptr ;  // Both mean the same.

const int* means pointer can point to a different location but the value in the location it is pointing at cannot be changed.

int* const means a const pointer. It cannot point to a different location but can change the value in the location it is pointing at. Since it is a constant pointer it must be initialized while declaration.

const int* const means a constant pointer to a constant value. It can neither point to different location nor change the value in the location it is pointing at. So, it must be initialized while declaration.

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.

If you understand the above errors, const int* and int* const can be easily understood. Hope it helps !

Mahesh
  • 34,573
  • 20
  • 89
  • 115
  • Hi Mahesh if my understanding si correct typedef const int* PointerToConstInt1; typedef const int *PointerToConstInt2; typedef int const* PointerToConstInt3; typedef int const *PointerToConstInt4; should all represent the same.... – RandomCPlusPlus Apr 08 '11 at 16:44
  • +1: This pretty much sums it all up correctly. – rubenvb Apr 08 '11 at 16:52
  • `typedef int const PointerToConstInt3;` `typedef const int PointerToConstInt2;` represent the same but they are not pointers. They are just typedef for constant integers. `const int PI = 3.147;` or `int const PI = 3.147;` – Mahesh Apr 08 '11 at 16:53
  • I believe PointerToConstInt3 is a pointer - check my post. I think You forgot the star in your comment. Cheers – RandomCPlusPlus Apr 08 '11 at 16:58
  • @Random - But in comment you are missing `*` in the typedef. You just mentioned in the comment as - `typedef const int PointerToConstInt2;`. See the first comment you made on my answer post. – Mahesh Apr 08 '11 at 17:00
  • @Mahesh - oh well you right. I copied and pasted from the post but the formatter must not like stars ;-). It toook to indicate italics I guess. Anyway PointerToConstInt1, PointerToConstInt2, PointerToConstInt3, PointerToConstInt4 are all the same and all pointers – RandomCPlusPlus Apr 08 '11 at 17:02
  • 1
    @RandomCPlusPlus - I think you grouped the different semantics perfectly in your code, with your commments. @Mahesh - double check your answer: `const int const*` should not be a constant pointer to a constant value, but a pointer to a constant value, which happens to be qualified twice. – rturrado Apr 08 '11 at 17:14
  • @rturrado - Thanks, modified. – Mahesh Apr 08 '11 at 17:18
  • @rturrado - Lovely, i think you hit the nail on the head. Trying to see if I understood what I read after posting typedef const int const* is a requalification as the compiler reads it as typedef int const const* --------------------------- typedef const int *const is NOT a requalification as the compiler reads it as typedef int const *const so the latter must be const pointer to const? Correct? – RandomCPlusPlus Apr 08 '11 at 17:26
  • 1
    @RandomCPlusPlus - correct. `typedef const int *const` is the same as `typedef int const *const`, a constant pointer to a constant value. I think the standard defines that `const` qualifies whatever is at its left hand side. However, for the typical `const type blah` expression, most compilers will recognize it to be the same as `type const blah`. For simplicity, just grab whatever the compiler reads, and read it from right to left. – rturrado Apr 08 '11 at 17:40