问题描述
我最近对代码进行了一些调整,其中我不得不更改函数中的形式参数.最初,该参数类似于以下内容(请注意,该结构先前是类型定义的):
I was recently making some adjustments to code wherein I had to change a formal parameter in a function. Originally, the parameter was similar to the following (note, the structure was typedef'd earlier):
static MySpecialStructure my_special_structure;
static unsigned char char_being_passed; // Passed to function elsewhere.
static MySpecialStructure * p_my_special_structure; // Passed to function elsewhere.
int myFunction (MySpecialStructure * p_structure, unsigned char useful_char)
{
...
}
之所以进行更改,是因为我可以在编译之前定义和初始化my_special_structure,而myFunction从未更改过它的值.这导致了以下更改:
The change was made because I could define and initialize my_special_structure before compile time and myFunction never changed the value of it. This led to the following change:
static const MySpecialStructure my_special_structure;
static unsigned char char_being_passed; // Passed to function elsewhere.
static MySpecialStructure * p_my_special_structure; // Passed to function elsewhere.
int myFunction (const MySpecialStructure * p_structure, unsigned char useful_char)
{
...
}
我还注意到,当我在程序上运行Lint时,有多个Info 818引用了许多不同的功能.该信息指出,可以将指针参数'x'(第253行)声明为指向const"..
I also noticed that when I ran Lint on my program that there were several Info 818's referencing a number of different functions. The info stated that "Pointer parameter 'x' (line 253) could be declared as pointing to const".
现在,关于上述内容,我有两个问题.首先,关于上述代码,由于MySpecialStructure中的指针和变量均未在函数中更改,因此将指针声明为常量是否有好处?例如-
Now, I have two questions in regards to the above. First, in regards to the above code, since neither the pointer nor the variables within MySpecialStructure is changed within the function, is it beneficial to declare the pointer as constant as well? e.g. -
int myFunction (const MySpecialStructure * const p_structure, unsigned char useful_char)
我的第二个问题是关于皮棉信息.如果函数不改变其值,则将指针声明为常量形式参数是否有任何好处或缺点...即使传递给函数的内容从未声明为常量也是如此?例如-
My second question is in regards to the Lint information. Are there any benefits or drawbacks to declaring pointers as a constant formal parameter if the function is not changing its value... even if what you are passing to the function is never declared as a constant? e.g. -
static unsigned char my_char;
static unsigned char * p_my_char;
p_my_char = &my_char;
int myFunction (const unsigned char * p_char)
{
...
}
感谢您的帮助!
为澄清起见而编辑-
将指向const
的指针或 const
指向const
的指针作为正式参数有什么好处 >?我知道我可以做到这一点,但是为什么我要...特别是在传递的指针及其指向的数据未声明为常量的情况下?
What are the advantages of declaring a pointer to const
or a const
pointer to const
- as a formal parameter? I know that I can do it, but why would I want to... particularly in the case where the pointer being passed and the data it is pointing to are not declared constant?
推荐答案
我认为您的意思是指向const
的指针.
I assumed you meant a pointer to const
.
通过使用指向const
的指针作为参数,优点是可以通过告诉程序员您的函数不修改指针所指向的对象来记录API.
By have a pointer to const
as a parameter, the advantage is you document the API by telling the programmer your function does not modify the object pointed by the pointer.
例如查看memcpy
原型:
void *memcpy(void * restrict s1, const void * restrict s2, size_t n);
它告诉程序员s2
指向的对象将不会通过memcpy
调用进行修改.
It tells the programmer the object pointed to by s2
will not be modified through memcpy
call.
它还提供了编译器强制执行的文档,因为如果您修改指向const
的指针的指针,实现将发出诊断信息.
It also provides compiler enforced documentation as the implementation will issue a diagnostic if you modify a pointee from a pointer to const
.
这篇关于将指向const的指针或指向const的const指针声明为形式参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!