Hey can anyone tell what this const means here???Code:void put_data(const TRAY *ptr);
Hey can anyone tell what this const means here???Code:void put_data(const TRAY *ptr);
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
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??
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
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.