Reputation: 1051
I am facing a function which takes a pointer to a char
array but it performs only read operations on the mem the char *
points to.
I could add const
to the prototype directly before the char *
parameter, but this would break the rest of the non const-correct code. I decided to add a comment /*const*/
instead to indicate that the function performs only read operations.
However, this confuses my colleagues and I wonder if adding the comments is a common approach and the criticism is unjustified.
Example:
int func(const char *readat);
versus
int func(/*const*/ char *readat);
Upvotes: 1
Views: 79
Reputation: 224082
Adding const
to the function parameter won't break any calling code. It is allowed to assign a non-const object to a const object.
So this is allowed:
char c;
char *p = &c;
const char *cp = p;
But this is not:
char c;
const char *cp = &c;
char *p = cp;
Upvotes: 3