Home > Back-end >  And an array, how to add const parameter?
And an array, how to add const parameter?

Time:11-19

Seems to have a saying, as much as possible before the parameters of const, prevent parameters change, I would like to array sum, array parameter is the starting address and ending address, these two address (pointer) is a constant in the function, but how to add? Below is the program I wrote

 
#include

using namespace std;

Const int ArSize=8;
Int sum_arr1 (const int & amp; The begin, const int & amp; End);
Int sum_arr2 (const int * begin, const int * end);


Int main ()
{
Int cookies [ArSize]=,2,4,8,16,32,64,128 {1};
Int sum_1=sum_arr1 (cookies [0], cookies [8]).
Int sum_2=sum_arr2 (cookies, cookies + 8);
cout cout
return 0;
}

Int sum_arr1 (const int & amp; The begin, const int & amp; End)
{
Int * pt;
Int total=0;
For (pt=& amp; The begin. Pt!=& amp; end; Pt++)
Total=total + * pt;

Return the total;
}

Int sum_arr2 (int * const begin, int * const end)
{
Int * pt;
Int total=0;
For (pt=begin; Pt!=end; Pt++)
Total=total + * pt;
Return the total;
}

CodePudding user response:

 # include 

using namespace std;

Const int ArSize=8;
Int sum_arr2 (const int * begin, const int * end);
Int main ()
{
Int cookies [ArSize]=,2,4,8,16,32,64,128 {1};
Int sum_2=sum_arr2 (cookies, cookies + 8);
cout return 0;
}
Int sum_arr2 (const int * begin, const int * end)
{
Const int * pt;
Int total=0;
For (pt=begin; Pt!=end; Pt++)
Total=total + * pt;
Return the total;
}

For a constant pointer and a pointer to constant difference, please refer to the "pointer to a constant and a constant pointer in c + +"
Wish I could help you!

CodePudding user response:

Const int * const, first modification of const int, the second const pointer, meaning: pointer to a constant integer constants

CodePudding user response:

Always a constant integer pointer

A constant, that is, itself, is A constant

B pointer, the pointer to the class, its type is B type

Often integer pointer, namely the contents of the pointer to the type is a common frame type , [a pointer to an integer and is constant]

always integer pointer constants, namely pointer pointing to the content of the type is a common frame type] , and the


const * const & lt;=> constant pointer constants

CodePudding user response:

There are two kinds of const to the pointer addition, has been the pointer itself is const, a value is a pointer to const

CodePudding user response:

Because you have to use inside the function of a non const int * to reference data so if you add const parameter is the function of internal (you should need casts)
You can directly use the
Const int the sum (const int * pbegin, const int * pend) {
int i=0;
While (pbegin!=pend)
I +=* pbegin++;
}
Such as

In fact already built similar features such as c + + c + + 20 so to write:

#include
#include
#include
Int main ()
{
Const int [x] {1, 2, 3, 4};
STD: : cout
}
On my machine with
under the CygwinG + + sum. Cc - STD=c + + 2 a
Can be normal work
Prev:c
  • Related