简体   繁体   English

const说明符,用于C中用户定义的数据类型

[英]const specifier to the user defined data type in C

I have user defined data type 我有用户定义的数据类型

typedef Unsigned int8  COMMAND_TYPE[6];

now I have function like 现在我有像

ConnectCommand(COMMAND_TYPE const command)
{
}

When try to run static analyzer check I get an Misra warning that "The object addressed by the pointer parameter 'command' is not modified and so the pointer could be of type 'pointer to const'" 尝试运行静态分析器时,我收到Misra警告,“指针参数'command'指向的对象未修改,因此指针的类型可能是'指向const的指针'”
This is violation of Misra rule 16.7 这违反了Misra规则16.7

Though const specifier is used and it appears its not seen here by the misra rule checkin tool and why ? 尽管使用了const说明符,但在misra规则检查工具中似乎看不到它,为什么?

Certain this is a static analyzer check problem. 当然这是一个静态分析器检查问题。

The below do the same and both should pass your checker. 以下操作相同,并且两者都应通过您的检查。

ConnectCommand(COMMAND_TYPE const command)
ConnectCommand(const COMMAND_TYPE command)

Although legal code, using a typedef of an array, confused your checker. 尽管使用数组的typedef的合法代码使您的检查程序感到困惑。 IMO, this is a design style best avoided. IMO,这最好避免的设计风格。


Making this community wiki for reference. 使该社区Wiki供参考。 Suggest deleting once aa good answer comes along. 建议您删除一个好的答案。

On review I suspect the problem is Misra - it is giving a false warning. 经过审查,我怀疑问题是Misra-它发出了错误的警告。 #2, and #3 commented below do the same thing. 下面评论的#2和#3做相同的事情。 #4 is redundant with #2, #3. #4与#2,#3是多余的。

I will be either deleting this answer or re-work it correctly. 我将删除此答案或对其进行正确的重新处理。

Suggest attempting 建议尝试

 
 
 
  
  typedef Unsigned int8 COMMAND_TYPE[6]; typedef const Unsigned int8 CONST_COMMAND_TYPE[6]; void ConnectCommand(CONST_COMMAND_TYPE command) { ... } COMMAND_TYPE c; ConnectCommand(c);
 
  

const in ConnectCommand(COMMAND_TYPE const command) { ...} refers to a const command . constConnectCommand(COMMAND_TYPE const command) { ...}是指一个 const command command is a pointer to objects of type COMMAND_TYPE . command是指向 COMMAND_TYPE类型的对象的指针。 const command implies that the pointer should not change in the ConnectCommand() function body. const command表示指针不应在 ConnectCommand()函数主体中更改。

The "const" in the warning "... type pointer to const" recommends using a const to the type of object pointed to by command . 警告“ ...指向const的类型指针”中的“ const”建议 command指向的对象类型使用 const This is because the data pointed to by command is not changed. 这是因为 command所指向的数据未更改。

 
 
 
  
  // add could drop this const // | | ConnectCommand(const COMMAND_TYPE const command)
 
  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM