Re: Declare an array parameter to be const?

From: Andrey Tarasevich (andreytarasevich_at_hotmail.com)
Date: 01/27/05


Date: Thu, 27 Jan 2005 10:30:55 -0800

Dylan Nicholson wrote:
> Is there any way of declaring the parameter "array" below to be const,
> so that the function as written will not compile:
>
> void foo(int array[] /*const*/)
> {
> array = array; // should not be allowed to point this at another
> array!
> }

As long as you are using this syntax, it can't be done in C++.

Note, that what you declare in this case is not an array but a mere
pointer. Which means that you can switch to the alternative (and
absolutely equivalent) way to declare the same parameter

  void foo(int* array);

and then add const qualifications as you please

  void foo(const int* array);
  void foo(int* const array); // <- this is the one you need
  void foo(const int* const array);

As a side note, in the C99 C language you can archive what you need with
your original syntax

  void foo(int array[const]);

but this is ill-formed in C++.

-- 
Best regards,
Andrey Tarasevich


Relevant Pages

  • Re: Help writing a new filter
    ... if we just told you the correct syntax for the array declaration you require and you didnt bother to learn about c# syntax then every single c# syntax error you ever got would result in a newsgroup post. ... This would be completely tedious for both you and the newsgroup. ... error, bad array declarator. ... To declare a managed array the rank ...
    (microsoft.public.win32.programmer.directx.video)
  • Re: .join() !== + a + b; // for some a and b
    ... The one seemed obvious until your more detailed explanation below, ... that made it necessary to look deeper into the syntax. ... one an Array object reference and one ... seems wrong with the expression evaluation on the left side of the logical ...
    (comp.lang.javascript)
  • Re: RPL+
    ... I'm upsetting the syntax here. ... If SIGN is an array, ... I understand that RPL in HPs also ... speed gap grew as the number of locals increases. ...
    (comp.sys.hp48)
  • Re: RPL+
    ... I'm upsetting the syntax here. ... C and RPL syntaxes are orthogonal. ... If SIGN is an array, ... speed gap grew as the number of locals increases. ...
    (comp.sys.hp48)
  • Re: SystemVerilog queue initialization using ncverilog +sv?
    ... found 'packed array' instead). ... the syntax ... Consequently, the "assignment pattern" syntax ... "unpacked array concatenation" to legitimize the syntax ...
    (comp.lang.verilog)