I would like to understand 'const unsigned char *' function parameters.
say, there is a library having a method like
SomeMethod(const unsigned char *data)
{..}
If I understand correctly, 'const' means one cannot change the value that the pointer points to. Does this mean, I have to define a variable, which will be eventually passed to this method, like
const unsigned char *value = "hello c++";
?
If I do, I can no longer change the 'value' variable above, correct?
But in general, function parameters take various values. And in my program the parameter 'data' changes dynamically.
Could anyone point out what I am missing? (or misunderstanding?)
If a function accepts a 'const T *', it means that the function promises not to modify the T. You are not required not to modify the data you passed to the function. It's okay to pass a pointer to non-const; 'T *' can be implicitly and safely converted to 'const T *'.
If a function returns a 'const T *', you are promising not to modify the T. Breaking this promise may invalidate some assumptions the code you're using made and the behavior of the program would become undefined.