473,406 Members | 2,377 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Declare an array parameter to be const?

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
6 10793
"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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Gianni Mariani | last post by:
Does anyone know if this is supposed to work ? template <unsigned N> int strn( const char str ) { return N; } #include <iostream>
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
1
by: Sam | last post by:
Hello all I have a two dimensional array (the dimensions are not known) that needs to be passed to fortran from c++, allocate the dimensions of the array in fortran code, do some filling up of...
16
by: Steven T. Hatton | last post by:
As far as I know, there is no way to provide a default value for the argument to the constructor A::A(char (&array)) in this example. Correct? struct A{ A(char (&array) ){...
5
by: Ingo Brueckl | last post by:
I need to declare a fixed array of (already defined and working) sort functions (sortfunc1, sortfunc2, sortfunc3) to pass one element of the array to qsort itself: int index; ??? func =...
5
by: Milan Cvetkovic | last post by:
Hi, Recently I came accross a weird bug in my code which turned to be my missunderstanding of arrays in C++. It all boils down to the small example: template<class ITER> const char* gggg...
3
by: johnmmcparland | last post by:
Hi all, I would like to have a static constant array inside a class definition which would contain the number of days in each month (I am writing a Date class as an exercise). However my...
2
by: vishwesha.guttal | last post by:
Hi, I am having troble declaring a const array. If the array size is small, then one can do as follows: const double array = {1, 2, 3, 4, 5}; What if I have an array of size say 1000 or...
10
by: Tammy | last post by:
Hello all, I am wondering what is the best way to declare a struct to be used in other c and c++ files. Such as for a C API that will be used by others. 1. Declaring the typedef and the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.