By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,127 Members | 1,135 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,127 IT Pros & Developers. It's quick & easy.

Declare an array parameter to be const?

P: n/a
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!
}
Jul 23 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
"Dylan Nicholson" <wi******@hotmail.com> 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?
Jul 23 '05 #2

P: n/a
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 );
}

Jul 23 '05 #3

P: n/a
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:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting

Jul 23 '05 #4

P: n/a
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
Jul 23 '05 #5

P: n/a
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 );
}
Jul 23 '05 #6

P: n/a
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
Jul 23 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.