Declare an array parameter to be const?

Discussion in 'C++' started by Dylan Nicholson, Jan 27, 2005.

  1. 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!
    }
    Dylan Nicholson, Jan 27, 2005
    #1
    1. Advertising

  2. "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' here is not an array. It's a pointer to int.

    > {
    > array = array; // should not be allowed to point this at another
    > array!
    > }


    The only way I know is to declare your function

    void foo(int * const array)

    (forget the [], they are misleading). That makes the [formal] argument
    const-qualified, which will make it non-changeable inside the function.

    V

    P.S. Was that an interview or a test question?
    Victor Bazarov, Jan 27, 2005
    #2
    1. Advertising

  3. 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!
    > }


    int b[3];


    void foo( int * const array )
    {
    array = b; // error
    }


    int main()
    {
    int a[3];


    foo( a );
    }
    Gianni Mariani, Jan 27, 2005
    #3
  4. Victor Bazarov wrote:

    > The only way I know is to declare your function
    >
    > void foo(int * const array)


    You can pass a [const] reference to the array which also guarantees
    that the function is called with a statically sized array (for
    whatever this may be a good thing):

    /**/ template <int size>
    /**/ void foo(int const (&array)[size])
    /**/ {
    /**/ //...
    /**/ }
    --
    <mailto:> <http://www.dietmar-kuehl.de/>
    <http://www.contendix.com> - Software Development & Consulting
    Dietmar Kuehl, Jan 27, 2005
    #4
  5. Dylan Nicholson

    Attila Feher Guest

    Dietmar Kuehl wrote:
    > Victor Bazarov wrote:
    >
    >> The only way I know is to declare your function
    >>
    >> void foo(int * const array)

    >
    > You can pass a [const] reference to the array which also guarantees
    > that the function is called with a statically sized array (for
    > whatever this may be a good thing):
    >
    > /**/ template <int size>
    > /**/ void foo(int const (&array)[size])
    > /**/ {
    > /**/ //...
    > /**/ }


    Change /**/ int const to /**/ char const and tada, you have got a function,
    which can be called with a string literal, but not with an ordinary char
    pointer. You can still use it with an char array, but that kind of use will
    (most probably) be rare. Of course such a function (commonality-variablity)
    would just forward to a simple char const *+size_t non-template version,
    which makes the real work. (Or to a public one, if you want to provide a
    ptr+length version.) That is one way such a construct can be used.

    --
    Attila aka WW
    Attila Feher, Jan 27, 2005
    #5
  6. Attila Feher wrote:
    ....
    >>You can pass a [const] reference to the array which also guarantees
    >>that the function is called with a statically sized array (for
    >>whatever this may be a good thing):
    >>
    >>/**/ template <int size>
    >>/**/ void foo(int const (&array)[size])
    >>/**/ {
    >>/**/ //...
    >>/**/ }

    >
    >
    > Change /**/ int const to /**/ char const and tada, you have got a function,
    > which can be called with a string literal, but not with an ordinary char
    > pointer. You can still use it with an char array, but that kind of use will
    > (most probably) be rare. Of course such a function (commonality-variablity)
    > would just forward to a simple char const *+size_t non-template version,
    > which makes the real work. (Or to a public one, if you want to provide a
    > ptr+length version.) That is one way such a construct can be used.


    If foo is a large-ish function, you're going to experience alot of code
    bloat if you do it that way. This might be a solution.

    class Foo
    {
    template <int size>
    friend void foo(char const (&array)[size]);

    static void DoFoo( const char * s, int size );
    };

    template <int size>
    inline void foo(char const (&array)[size])
    {
    Foo::DoFoo( const char * array, size );
    }
    Gianni Mariani, Jan 27, 2005
    #6
  7. 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
    Andrey Tarasevich, Jan 27, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. George Ter-Saakov

    how do i declare const array?

    George Ter-Saakov, Aug 22, 2003, in forum: ASP .Net
    Replies:
    4
    Views:
    3,620
    George Ter-Saakov
    Aug 22, 2003
  2. Spoon
    Replies:
    6
    Views:
    497
  3. Replies:
    2
    Views:
    488
    Ivan Vecerina
    Apr 24, 2007
  4. Javier
    Replies:
    2
    Views:
    532
    James Kanze
    Sep 4, 2007
  5. Replies:
    5
    Views:
    615
Loading...

Share This Page