Const array const {}

So you can do this:

void foo(const int * const pIntArray, const unsigned int size); 

Which indicates that the arrival of the pointer is read-only, and the integer that it points to is read-only.

You can access this inside the function as follows:

 blah = pIntArray[0] 

You can also make the following announcement:

 void foo(const int intArray[], const unsigned int size); 

This is almost the same, but you can do this:

 intArray = &intArray[1]; 

Can i write:

 void foo(const int const intArray[], const unsigned int size); 

Is it correct?

+7
source share
3 answers

No, your last option is incorrect. What you are trying to do is achieved on C99 with the following new syntax

 void foo(const int intArray[const], const unsigned int size); 

which is equivalent

 void foo(const int *const intArray, const unsigned int size); 

The syntax [const] specific to C99. It is not valid in C89 / 90.

Keep in mind that some people think that top-level cv qualifiers are β€œuseless” in functional parameters because they qualify a copy of the actual argument. I do not consider them useless at all, but personally I do not meet too many reasons to use them in real life.

+15
source

Use cdecl . This gives an error in the second entry. The first only clearly assumes that the second const refers to * .

+1
source

In C / C ++, you cannot pass an entire array as an argument to a function. You can, however, pass functions to a pointer to an array by specifying the name of the array without an index.

(for example) This fragment of the program passes the address I to func1 ():

 int main(void) { int i[10]; func1(i); . . . } 

To get i, the function func1 () can be defined as

 void func1(int x[]) /* unsized array */ { . . } 

or

 void func1(int *x) /* pointer */ { . . } 

or

 void func1(int x[10]) /* sized array */ { . . } 

source: FULL LINK - HERBERT.

0
source

All Articles