different between const int*** and int** const

This is a discussion on different between const int*** and int** const within the C++ Programming forums, part of the General Programming Boards category; i've continues test come around multi-level pointers, and got trouble too: Code: int main() { const int*** x; x = ...

  1. #1
    Registered User PuKa's Avatar
    Join Date
    Oct 2011
    Posts
    23

    different between const int*** and int** const

    i've continues test come around multi-level pointers, and got trouble too:
    Code:
    int main() {
      const int*** x;
      x = new const int**;
      *x = new const int*;
      **x = new int;
      // const pointer
      int*** const y = new int**;
      *y = new int*;
      **y = new int;
      return 0;
    }
    my question is why and where i need use const in allocation a pointer to constant, and why i don't need const in allocation of a constant pointer?

    thanks for reply.

  2. #2
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  3. #3
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,235
    The difference is whether the pointer is constant, or the thing being pointed to is constant.

    Code:
    const int *x; // x is a pointer to a CONSTANT integer
    int * const x; // x is a CONSTANT pointer to an integer
    In the first case, the following statement would be illegal:

    Code:
    *x = 1;
    In the second case, the following statement would be illegal:

    Code:
    x = &y;
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  4. #4
    Registered User PuKa's Avatar
    Join Date
    Oct 2011
    Posts
    23
    No, i mean why *x = new const int*; has 'const' if not error occur , **x = new int; hasn't 'const' if has error occur.

    sorry for my description.

  5. #5
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,235
    Quote Originally Posted by PuKa View Post
    No, i mean why *x = new const int*; has 'const' if not error occur , **x = new int; hasn't 'const' if has error occur.

    sorry for my description.
    For the reason I just stated.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #6
    Registered User PuKa's Avatar
    Join Date
    Oct 2011
    Posts
    23
    const int*** x;
    x = new const int**; // if i use this statement x = new int**; // error: invalid conversion from 'int***' to 'const int***' , ok i understand this.
    *x = new const int*; // the same as previous line.
    **x = new int; // this is problem. if i use this statement **x = new const int; error occur, why two lines above must be have 'const' while this line must be not have 'const'

    I use g++ ver 4.3 to compile.

    Mod, please change my topic'name by "Keyword 'const' in allocate memory a multi-level pointers" thanks a lot.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,428
    >>**x = new int; // this is problem. if i use this statement **x = new const int; error occur, why two lines above must be have 'const' while this line must be not have 'const'
    Separate compile errors from runtime errors.
    The line must have const because you have a pointer to const int.
    But as x is uninitialized in your code, dereferencing it is a no-no.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User PuKa's Avatar
    Join Date
    Oct 2011
    Posts
    23
    1. i still don't understand, as you said and error compile i didn't initialize a value to ensure has got a memory area is allocated and initialized, but how can i do two jobs in the same time?

    2. what about these statement
    int*** const y = new int**;

    *y = new int*;

    **y = new int;
    why i don't need const in act allocation, if need then where i put 'const' in these statement.

    thanks!

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,428
    1) I don't understand what this question is trying to ask.
    2) Go back to brewbuck's example. It explains well the two type of "const" available.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User PuKa's Avatar
    Join Date
    Oct 2011
    Posts
    23
    *x = new const int*; // no error
    *x = new int*; // error

    **x = new int; // no error
    **x = new const int; // error

    i've just wanted to ask why i declares *x = new const int*; is OK while **x = new const int; is occur error. hic.

  11. #11
    Registered User
    Join Date
    Apr 2006
    Posts
    2,021
    Quote Originally Posted by PuKa View Post
    *x = new const int*; // no error
    *x = new int*; // error

    **x = new int; // no error
    **x = new const int; // error

    i've just wanted to ask why i declares *x = new const int*; is OK while **x = new const int; is occur error. hic.
    Because new takes a non-const type, a type that does not match "const T". const int matches that form. const int * does not, the pointer is not const, it just points to const data.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  12. #12
    Registered User PuKa's Avatar
    Join Date
    Oct 2011
    Posts
    23
    yes, it's basically, but... i did not understand it. thanks much.

  13. #13
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,428
    What King Mir is trying to say is that new takes a modifiable type. T is a type that you can modify. const T is not a type you can modify.
    Hence, x = new int is okay, while x = new const int is not.
    Now, separate the constness for pointers. There is const int* which is a type that points to an int. You can still change the variable itself, but you can't change what it points to.
    Example:

    int a, b;
    const int * x = &a;
    x = &b; // OK
    *x = 10; // Not OK; x points to const int, hence *x is a const int.

    There is also the type int* const which is is a constant type that points to int. It means you can't change the variable itself, but you can change what it points to.
    Example:

    int a, b;
    const * const x = &a;
    x = &b; // Not OK; x is const.
    *x = 10; // OK
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Registered User PuKa's Avatar
    Join Date
    Oct 2011
    Posts
    23
    Quote Originally Posted by Elysia View Post
    int a, b; const * const x = &a; x = &b; // Not OK; x is const. *x = 10; // OK
    I think *x = 10 is illegal too, x is a const pointer points to const, so can't modify value that x points to through *x.

  15. #15
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,428
    Ah, no, that was a typo.
    It should say
    int * const x = &a;
    there.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 04-20-2011, 01:19 PM
  2. Replies: 3
    Last Post: 11-15-2009, 03:57 AM
  3. Replies: 1
    Last Post: 04-03-2009, 08:52 AM
  4. Replies: 7
    Last Post: 04-28-2008, 09:20 AM
  5. Mutable members in const member functions still const
    By ripper079 in forum C++ Programming
    Replies: 3
    Last Post: 10-23-2002, 08:56 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21