Reputation: 76
I need to know the advantages that a programmer has by making a function return type as 'const' qualified in C. This question is not related to C++.
Following function signature is a valid C function signature, so the creators allowed it keeping some use case in mind, which I cannot find anywhere in web.
const int foo(int arg);
I know in C++ it has a lot of uses to make returned objects immutable or read-only because there can be accidental assignments to returned object.
Upvotes: 1
Views: 166
Reputation: 73366
const
makes no sense for return values because return values are rvalues in any case and can't be modified.
Src.
An exception: const T*
can be useful, since it mentions that the object the pointer is pointing to is constant, and the user of the function should respect that.
Upvotes: 4
Reputation: 2748
Consider the following case:
const char* F() {
return " aaaaaaaaa. Ddddd";
}
Upvotes: -3