I have a char * that is actually 10 digit string. I basically take that in my function and spit out a code. Now i have made a static lib that i will share with someone else…the question is ( i am using c++)
for the definition of that api that i will provide in my header
do i just put const char * const or not. I am not sure if i need to do it or not. I just figured if they were going to use that API I did not want them to by mistake in their code send some bad pointer or value to my code. so to protect myself i am making it like i stated above is that the right approach? is that how one would use const?
Making something const does not mean that you won’t get a bad pointer. There’s nothing you can do to guard yourself against that. You can check for NULL, but not a freed pointer or a corrupted one.
What const says is that your function promises not to change the data that is pointed to. That’s useful for the caller to know. Also, in cases where the caller only has access to const pointers, you are saving them a cast or copy to call your function.
The normal usage is
Which means that the values pointed to by s won’t be changed.