Const pointer to mutable fixed size array

Hi, so the question is.. is there any way to write that?
Other than this:
1
2
T a[42];
T * const b = a;


For instance, how would you write that a function expects such a pointer in input?
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:

void function(int*const t) { // ...

the two calls in main would still be allowed.
Hi cire,

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(const int*).

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?


I have no clue what you're talking about.
However that would resolve a lot of stupid errors that programmers do.
By instance
1
2
3
void list::add(node *position, const T &value){
   position = new node(value); //sigh
}

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.
There is no difference between those.
Thanks a lot for your last answers Zhuge and cire :)
I guess that answers the question then; if my intention is to do something like:
 
void myfunction( T a const [42] );

then I lose nothing of what was intended by writing
 
void myfunction( T * const a );


@ne555: I understand your post as an answer to cire. But just to be sure, in your example nothing prevents you from changing to:
1
2
3
void list::add(node * const position, const T &value){
   position = new node(value); // you won't be compiled, you silly instruction!
}


Cheers
Last edited on
If you really need to void foo( T (&array) [42] );
Topic archived. No new replies allowed.