Why would you write a function that expects such a pointer in input? Pointers are passed by value.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
void function(int* t)
{
// Changing what t points to doesn't change what the argument fed
// to function points to.
}
int main()
{
int a[42] ;
int * const b = a ;
int * c = a ;
function(b) ; // legal
function(c) ; // legal
}
If you were to change the definition of function to:
Truthfully, it was more out of curiosity than anything else.
- I know putting a regular pointer in the prototype would allow to pass fixed sized arrays as well.
- I know the pointer actually passed to the function is a copy of the pointer passed in the calling entity (t is a copy of b).
Specifying a const pointer in the prototype is a way to say that whatever the implementation does, the input pointer will always remain what was passed in the calling entity, which is can be handy I think.
Actually, I'm not sure exactly for who is supposed to be the "fixed size specification" of pointers; is it for us (modest humans)? Does the compiler use that to detect bugs? Does it affect runtime at all?
Maybe my lack of knowledge about these previous points is the real reason to my question...
Specifying a const pointer in the prototype is a way to say that whatever the implementation does, the input pointer will always remain what was passed in the calling entity, which is can be handy I think.
I guess, but functions are (in essence) a sort of a black box anyway, and since you don't actually gain anything my specifying the constness of the pointer itself, I don't see much point.
"fixed size specification" of pointers
What do you mean? Where are we specifying anything about what the pointer can point to to? Am I misunderstand what you're asking here...?
Specifying a const pointer in the prototype is a way to say that whatever the implementation does
Specifying a const pointer in the declaration is pointless and silly. The caller doesn't care what the function does with it's copy of the parameter, nor should it. If I see that prototype in a header my first thought is: I'd better check to see if that should be void function(constint*).
If, for some reason, you think your compiler needs the hint that you aren't going to change the parameter in the function definition in order to optimize, you could (just) supply the const modifier in the definition, and not in the declaration.
Actually, I'm not sure exactly for who is supposed to be the "fixed size specification" of pointers; is it for us (modest humans)? Does the compiler use that to detect bugs? Does it affect runtime at all?
Specifying a const pointer in the declaration is pointless and silly.
That's a little rough! :D Yes, my last question was a little unclear, sorry for that, and thanks for your two answers by the way.
I'm not talking about "fixed size array specification" in the body of an implementation. The use there is quite straightforward, since a known array size at compile time avoids dynamic allocations for instance.
I'm talking about fixed size array specification in prototypes; how does
void myfunction( T *a );
differ from
void myfunction( T a[42] );
practically? Does the compiler make sure that there are at least 42 elements being pointed to at each function call?
IIRC the standard makes absolutely no distinction between those two definitions; they are exactly equivalent. I suppose a particular compiler might use that information for checking, but that would be an extension.