3
#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. 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:

Attempt to cast away const (or volatile)
Lundin
  • 195,001
  • 40
  • 254
  • 396
kapilddit
  • 1,729
  • 4
  • 26
  • 51
  • 4
    Do not put a `;` after your macro definition. Otherwise your question is not clear, could you reshape it? – ouah Apr 21 '14 at 10:58
  • possible duplicate of [difference between a macro and a const in c++](http://stackoverflow.com/questions/6393776/difference-between-a-macro-and-a-const-in-c) – Vovanium Apr 21 '14 at 11:02
  • You ask different questions in the title and the question description. Please be clear. – ajay Apr 21 '14 at 11:03
  • Sorry for improper description. Corrected some information. – kapilddit Apr 21 '14 at 11:05
  • Please see answers on other questions on this topic: http://stackoverflow.com/questions/6442328/what-is-the-difference-betweem-define-and-const http://stackoverflow.com/questions/6274008/why-would-someone-use-define-to-define-constants http://stackoverflow.com/questions/11153156/define-vs-const-in-objective-c/11154217 http://stackoverflow.com/questions/4024318/why-do-most-c-developers-use-define-instead-of-const/4024634 – Vovanium Apr 21 '14 at 11:07
  • The lint error is probably fixed by changing `char` to `char const` – M.M Apr 21 '14 at 11:09
  • @Vovanium Those links are completely unrelated. He is asking why Lint keeps giving him very strange errors. – Lundin Apr 22 '14 at 06:36
  • I don't think it is fair to bandwagon down-vote this. There is nothing in this code which will prevent it from compiling, the question is about why the static analysis tool gives strange error messages. – Lundin Apr 22 '14 at 06:39
  • @Lundin these links was related (in previous version) – Vovanium Apr 22 '14 at 15:15
  • @Vovanium No, they were only related to the previous weird title, not to the contents of the question. – Lundin Apr 23 '14 at 06:37

3 Answers3

8

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. They are not statements in the sense that they are not 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". String literal are read-only and it's undefined behaviour to try to modify them. Therefore, you should make the pointer const qualified.

#define MAXSTR "Maximum number reached"

const char *str = MAXSTR;  // make the pointer const
ajay
  • 9,402
  • 8
  • 44
  • 71
  • I tried with const char* str then error is: Attempt to cast away const (or volatile) – kapilddit Apr 21 '14 at 11:10
  • 1
    @kapilddit You have to initialize `const` objects, i.e., define and assign them in the same statement. You cannot define and then assign it later in a separate statement. – ajay Apr 21 '14 at 11:12
  • @ajay `#define MAXSTR "Maximum number reached"` `const char *str = MAXSTR;`. Why would this give an error? I am still not sure of this. – Kraken Apr 21 '14 at 12:26
6

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".

Attempt to cast away const (or volatile)

The warning is incorrect. Again, it should tell you that the pointer variable needs to be 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 []. They are unfortunately not treated as constant type const char[] by the language. 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. So you must treat string literals as if they were const arrays, even though they are not.

Therefore, it is good practice to always declare pointers to string literals as const char*.

In the case of Lint, it seems to treat string literals as if they were const char[], which they are not. Therefore it gives you incorrect errors instead of pointing out the actual problem.

Lundin
  • 195,001
  • 40
  • 254
  • 396
0

you can copy the string, just make sure it has enough space:

#define MAXSTR "Maximum number reached"

char str[100];

strcpy(str, MAXSTR);