我知道可以有几种方式声明const指针:
const int * intPtr1; // Declares a pointer that cannot be changed.
int * const intPtr2; // Declares a pointer whose contents cannot be changed.
// EDIT: THE ABOVE CLAIMS ARE INCORRECT, PLEASE READ THE ANSWERS.
但是将同样的原则应用于函数参数有什么影响呢?
我认为以下代码是多余的:
void someFunc1(const int * arg);
void someFunc2(int * arg);
因为 someFunc1 和 someFunc2 对指针本身采用值传递,所以在函数调用中,someFunc1 无法更改原始指针的值。 举个例子:
int i = 5;
int * iPtr = &i;
someFunc1(iPtr); // The value of iPtr is copied in and thus cannot be changed by someFunc1.
如果这些条件成立,那么用'const int * ptr'类型的参数声明函数将没有任何意义,对吗?