Thread: What does "const" in function parameter do ?

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    30

    What does "const" in function parameter do ?

    I'm not sure. What's the difference between:

    Code:
    int somemethod(char *a1, char *a2)
    ...and:

    Code:
    int somemethod(const char *a1, const char *a2)
    ?

    Best wishes, Desmond5

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    the second function notifies the caller that it will not modify the contents of the buffer pointer by their parameters. As a result - programmer will know that it is safe to pass string literal like "Hello" into this function.

    The first function may modify the buffer, so generally speaking - to pass string literal into it - it should be first copied into modifiable buffer.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Const tells the compiler that you cannot and will not change the contents of the variables to which the const applies.
    Note that you can have a pointer to a const type and a const pointer itself. This example uses a pointer to a const type, which is more common since it will guard the contents pointed to by the pointer from being changed.
    Const is partly to stop silly mistakes and partly to also enable some optimizations for the compiler.
    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.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Const is partly to stop silly mistakes and partly to also enable some optimizations for the compiler.
    I don't think any modern compilers would actually optimize a program based on whether const was present or not . . . it's kind of like the register keyword. I'm not sure on this, but that's what I think.

    As far as I know, const is indeed merely to help the compiler point out where you're doing something you told it you shouldn't be.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    I have read of at 1 case a while ago where VS (2005 I think) does generate slightly more optimal code when const is used. It was not for a function parameter though.
    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
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582
    Const forbids the change of the variable. While you could have some math upon initiating it, it cannot be changed after this. I have a few of these in my functions, usually as local variables and string parameters. Other globals have it as well are basically quick conversions. Here's a few examples:

    Code:
    const double pi = 3.1415926535897932; // you should recognize this one
    double degrees = 57.295779513082321// converts radians to degrees when multiplying
    
    int ProcessText(const char string[100], other parameters)
    {
    	short Variable; // uninitialized
    	const long UnchangingVariable = 8; // a local that can't change
    	const float NonchangingVariable = 32.8*degrees; // a local with some math used that can't change after this
    }
    High elevation is the best elevation. The higher, the better the view!
    My computer: XP Pro SP3, 3.4 GHz i7-2600K CPU (OC'd to 4 GHz), 4 GB DDR3 RAM, X-Fi Platinum sound, GeForce 460, 1920x1440 resolution, 1250 GB HDD space, Visual C++ 2008 Express

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by ulillillia View Post
    Const forbids the change of the variable.
    Not exactly. In C I tend to read 'const' as 'read-only'.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582
    Quote Originally Posted by Dave_Sinkula View Post
    Not exactly. In C I tend to read 'const' as 'read-only'.
    It would be the same. If something is read-only, that means that it cannot be changed and "const forbids the change of the variable" means that the value of the variable cannot be changed. They mean the same thing, though your "read-only" approach is much clearer.
    High elevation is the best elevation. The higher, the better the view!
    My computer: XP Pro SP3, 3.4 GHz i7-2600K CPU (OC'd to 4 GHz), 4 GB DDR3 RAM, X-Fi Platinum sound, GeForce 460, 1920x1440 resolution, 1250 GB HDD space, Visual C++ 2008 Express

  9. #9
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    read-only var could be changed in other part of the code, where the access is done without const qualifier
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  10. #10
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by ulillillia View Post
    It would be the same. If something is read-only, that means that it cannot be changed and "const forbids the change of the variable" means that the value of the variable cannot be changed.
    What is your view of something like this, then?
    Code:
    void foo(const volatile unsigned char *bar) { /* ... */ }
    Since I've had need to use it, I already understand why I specified 'bar' that way.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Replies: 13
    Last Post: 08-24-2006, 12:22 AM
  4. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM