简体   繁体   English

#define一个字符串文字,然后将其分配给一个char *

[英]#define a string literal then assign it to a char*

#define MAXSTR "Maximum number reached"

char *str = MAXSTR;

While doing this kind of operation. 在做这种操作的同时。 Code is working & running fine but I am getting lint error. 代码工作正常,但我得到lint错误。 How I can resolve it? 我怎么解决它?

Error:
Assignment of string literal to variable 

If I use: 如果我使用:

 #define MAXSTR "Maximum number reached"

 char *str = (char *) MAXSTR;

then lint error: 然后lint错误:

Attempt to cast away const (or volatile)

A macro is a name given to a fragment of code. 宏是给代码片段的名称。 Wherever the name occurs in the source file, it is replaced by the code fragment. 名称出现在源文件中的任何位置,都由代码片段替换。 Macro definitions are directives to the C preprocessor. 宏定义是C预处理器的指令。 They are not statements in the sense that they are not executed . 从某种意义上讲它们不是executed语句。 They are not even there after the preprocessing stage and hence generate no assembly code. 它们在预处理阶段之后甚至不存在,因此不生成汇编代码。

MAXSTR is a macro which is replaced by the string literal "Maximum number reached" . MAXSTR是一个宏,由字符串文字"Maximum number reached"代替。 String literal are read-only and it's undefined behaviour to try to modify them. 字符串文字是只读的,尝试修改它们是未定义的行为。 Therefore, you should make the pointer const qualified. 因此,您应该使指针const合格。

#define MAXSTR "Maximum number reached"

const char *str = MAXSTR;  // make the pointer const

Assignment of string literal to variable 将字符串文字赋值给变量

That is a horrible error message. 这是一个可怕的错误信息。 I'm curious about what Lint thinks string literals are good for, if we cannot assign them to variables... It should say something like: "assigning string literal to a non-constant pointer". 我很好奇Lint认为字符串文字是有益的,如果我们不能将它们分配给变量......它应该说:“将字符串文字分配给非常量指针”。

Attempt to cast away const (or volatile) 尝试抛弃const(或volatile)

The warning is incorrect. 警告不正确。 Again, it should tell you that the pointer variable needs to be const . 同样,它应该告诉你指针变量需要是const

To sum it up, you get those errors because your static analyser tool is bad. 总结一下,你会得到那些错误,因为你的静态分析器工具很糟糕。


Explanation: 说明:

String literals in C are character arrays, char [] . C中的字符串文字是字符数组char [] They are unfortunately not treated as constant type const char[] by the language. 遗憾的是,它们不被语言视为常量类型const char[] This is a defect in the C language, because if you attempt a write access of a string literal, then it leads to undefined behaviour and the program might crash and burn. 这是C语言中的缺陷,因为如果您尝试对字符串文字进行写访问,则会导致未定义的行为,并且程序可能会崩溃并烧毁。 So you must treat string literals as if they were const arrays, even though they are not. 因此,您必须将字符串文字视为const数组,即使它们不是。

Therefore, it is good practice to always declare pointers to string literals as const char* . 因此,最好将指向字符串文字的指针声明为const char*

In the case of Lint, it seems to treat string literals as if they were const char[] , which they are not. 在Lint的情况下,它似乎将字符串文字视为const char[] ,而它们不是。 Therefore it gives you incorrect errors instead of pointing out the actual problem. 因此,它会为您提供错误的错误,而不是指出实际问题。

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

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