I'm not sure. What's the difference between:
...and:Code:int somemethod(char *a1, char *a2)
?Code:int somemethod(const char *a1, const char *a2)
Best wishes, Desmond5
I'm not sure. What's the difference between:
...and:Code:int somemethod(char *a1, char *a2)
?Code:int somemethod(const char *a1, const char *a2)
Best wishes, Desmond5
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
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.
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.Const is partly to stop silly mistakes and partly to also enable some optimizations for the compiler.
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.
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"
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. 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.*
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
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
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.*