question about const pointers and const ints

This is a discussion on question about const pointers and const ints within the C Programming forums, part of the General Programming Boards category; I was just browsing through a few C exercises, came across this: The following code: Code: void main() { int ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    13

    question about const pointers and const ints

    I was just browsing through a few C exercises, came across this:

    The following code:

    Code:
    void main()
    {
                int  const * p=5;
                printf("%d",++(*p));
    }
    says it will give compiler error.
    But doesnt int const *p mean, constant pointer to an int ??
    So, *p dereferences the pointer and ++ increments the value.
    Shouldnt the answer be 6 ??

    If the statement were like this:

    const int * p=5;
    then ++(*p) would have given an error.

    That is just my explanation. feel free to throw light on the same.

    Thanks !

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    const binds to the left or right, so const int and int const are both the same thing. If you really want to say that your pointer is const, then you should have
    Code:
    int * const p ...
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    2

    Thumbs up

    Quote Originally Posted by matsp View Post
    const binds to the left or right, so const int and int const are both the same thing. If you really want to say that your pointer is const, then you should have
    Code:
    int * const p ...
    --
    Mats
    The easiest way to remember "const"'s binding direction is to learn this phrase: "const always binds to the left except when it preceeds a type". If you remember that, you'll always know which direction it is binding. All other modifiers bind to the right.

    Cheers,

    - Wil

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by WilBloodworth View Post
    The easiest way to remember "const"'s binding direction is to learn this phrase: "const always binds to the left except when it preceeds a type". If you remember that, you'll always know which direction it is binding. All other modifiers bind to the right.

    Cheers,

    - Wil
    Or by simply reading it as you normally do: a variable type points to whatever is left of the star.

    So:
    Code:
    const int* ptr;
    ptr is a variable that points to "const int". Whereas:

    Code:
    int * const ptr;
    Is a pointer to "int".

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,305
    Quote Originally Posted by WarDoGG View Post
    Code:
    void main()
    {
                int  const * p=5;
                printf("%d",++(*p));
    }
    says it will give compiler error.
    If it gives a compile-time error then it could also be because main must return int, not void.

    Assuming the const and return type were fixed, the main thing that nobody else has commented on yet is that the p=5 bit actually sets the pointer itself to point to memory address 5. When the next line dereferences 5 to see what is in that memory location, you should get a crash under Windows, as all memory addresses below 65536 are not dereferenceable.
    Thus the above snippet would also be a run-time error.
    Last edited by iMalc; 01-07-2011 at 08:56 PM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    455
    But doesnt int const *p mean, constant pointer to an int ??
    It's pointer-to-const-integer, quite different from constant pointer to integer. It will crash not because of 'const' but because you assigning p to an invalid memory. You can only do this with a variable. The purpose of this is just to prevent you from using the pointer to change the value of whatever the pointer points to. Remember that pointer stores address, so since the pointer itself is not const, you can change the value that pointer store, which is the address of another variable.
    Code:
    int var_1 = 5; 
    int var_2 = 6; 
    int const * p = &var; // works
    p = &var_2; // works; 
    *p = 7; // fails, can't use pointer to change value pointed to. 
    
    int const *p = malloc( sizeof (int) ) ; 
    *p = 5; // this will fail.
    One thing though, var_1 and var_2 don't have to be const. You use "const" just to prevent the pointer 'p' from changing the values of that object it points to.

    Moreover,
    Code:
    int const * p; 
    const int * p;
    These are equivalent. it just says that 'p' points to a constant object. you can't use *p to change the value that 'p' point too. However ++p works since 'p' itself is not const.
    Code:
    int * const p = malloc( sizeof( int ) ) ;
    p++; // this will fail because p is const pointer
    p = &var_1; // fails; 
    p = &var_2; // fails; 
    *p = 7; // works
    if you use the declaration above, then 'p' is a const pointer. in this case 'p++' or '++p' will fail because the location that 'p' is const. but "*p = 10" is ok.

    Code:
    int const * const p = &var_1;
    p++; // fails;
    p = &var_2; // fails; 
    *p = 10; // fails;
    You can make 'p' a constant pointer to const object by using the code above. One thing though, var_1 and var_2 don't have to be const. You use "const" just to prevent the pointer 'p' from changing the values of that object it points to.
    "All that we see or seem
    Is but a dream within a dream." - Poe

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,305
    I forgot to mention that you will also get a warning "assignment makes pointer from integer without a cast" if you let your compiler warn you. This particular warning is one that you should never ignore really, as it's virtually guaranteed to be a mistake.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    Registered User
    Join Date
    Jan 2011
    Posts
    2

    Thumbs up

    Quote Originally Posted by EVOEx View Post
    Or by simply reading it as you normally do: a variable type points to whatever is left of the star.

    So:
    Code:
    const int* ptr;
    ptr is a variable that points to "const int". Whereas:
    That's not how it should read. It should read, "ptr is a pointer to a constant int."... meaning you can change where ptr points to but you can't change the value stored at the location where ptr points.

    Quote Originally Posted by EVOEx View Post
    Code:
    int * const ptr;
    Is a pointer to "int".
    Again, "ptr is a constant pointer to an int". Meaning you can't change where ptr points to but you can change the value stored at the location where ptr points.

    - Wil

  9. #9
    Registered User
    Join Date
    May 2008
    Posts
    10
    Below link might be helpful for understanding declarations:

    Clockwise/Spiral Rule

  10. #10
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by WilBloodworth View Post
    That's not how it should read. It should read, "ptr is a pointer to a constant int."... meaning you can change where ptr points to but you can't change the value stored at the location where ptr points.



    Again, "ptr is a constant pointer to an int". Meaning you can't change where ptr points to but you can change the value stored at the location where ptr points.

    - Wil
    How's that different from what I said? Yes, I did say it with "baby words", because it was an introduction of a concept rather than a standardised method of remembering.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Use of 'const' with pointers to pointers
    By Sharke in forum C Programming
    Replies: 7
    Last Post: 03-27-2009, 12:15 AM
  2. Need help with const pointers
    By slickshoes in forum C++ Programming
    Replies: 9
    Last Post: 08-02-2007, 06:03 PM
  3. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  4. Memory leak - need help finding
    By ChadJohnson in forum C++ Programming
    Replies: 8
    Last Post: 04-06-2005, 07:26 PM
  5. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 12:26 AM

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