"const"

This is a discussion on "const" within the C Programming forums, part of the General Programming Boards category; Code: void put_data(const TRAY *ptr); Hey can anyone tell what this const means here???...

  1. #1
    Registered User
    Join Date
    Jan 2010
    Location
    on some of the worst place on earth
    Posts
    105

    "const"

    Code:
    void put_data(const TRAY *ptr);
    Hey can anyone tell what this const means here???

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by rakeshkool27 View Post
    Code:
    void put_data(const TRAY *ptr);
    Hey can anyone tell what this const means here???
    Basically; "this function won't modify the contents ptr points to". So it is some kind of guarantee ptr isn't changed in this function (though, actually, it is possible it still does, but that means the function has been programmed very, very badly).

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    It's basically for the internals of the function. A const, as you (hopefully) know, cannot be changed.

    That does not mean you have to submit a const variable -- it means that the function will not alter the ptr. You can submit a plain TRAY*, it will be "converted" to a const TRAY* automatically.

    A lot of standard library functions are written this way so that, as EVOEx points out, the user can tell from the prototype that the submitted pointer will be treated as const.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by MK27 View Post
    A lot of standard library functions are written this way so that, as EVOEx points out, the user can tell from the prototype that the submitted pointer will be treated as const.
    Another good reason to use it is that you, as a function writer, don't accidentally modify the variable. I can't say I've never had a compiler error because of something like that.

  5. #5
    Registered User
    Join Date
    Jan 2010
    Location
    on some of the worst place on earth
    Posts
    105
    So you mean const is used only for the safeside of the pointer inside a function.
    So it can be omitted also,not strictly required,right??
    So whether const can be used with other datatypes also other than pointers??

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    const can be used with any data type.
    You should always make your functions const-correct for safety & code clarity.
    If you don't intend on changing the value of a variable, make it const.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    2,053
    It's particularly important to pointer arguments though. You want functions that take pointer arguments to use const when they do not modify the value pointed to so that a const pointer could be passed to them as an alternative to a non const pointer. const correctness with local variables is less important.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What does "const" in function parameter do ?
    By desmond5 in forum C Programming
    Replies: 9
    Last Post: 02-23-2008, 04:07 AM
  2. confused by "const"
    By AntiScience in forum C++ Programming
    Replies: 14
    Last Post: 10-19-2007, 02:02 PM

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