![]() |
![]() |
0752 | ![]() |
||||
![]() | |||||||
| Pointers | REFERENCE - ISO:C90-6.1.4 String Literals - Semantics | ||||||
A string literal is of type "pointer to array of char". It is an unfortunate weakness of the C language (a result of its history), that the type is not "pointer to array of const char". The array of characters addressed by a string literal should never be modified and any attempt to do so will result in undefined behaviour. Message 0752 identifies situations where a string literal is passed as an argument to a function whose parameter is not defined as a "pointer to const". Message 0753 identifies situations where a string literal is assigned to a pointer which is not defined as a "pointer to const". For example:
/*PRQA S 2017,3122,3123,3204,3205,3408,3447,3602,3625 ++*/
extern void ef1(char * p);
extern void ef2(const char * p);
extern void foo(void)
{
char *pca = "xyz"; /* Message 0753 */
const char *pcb = "abc"; /* */
*pca = 'X'; /* Undefined */
*pcb = 'X'; /* Message 0556 - Constraint Error */
ef1("123"); /* Message 0752 */
ef2("456"); /* */
ef1(pcb); /* Message 0431 - Constraint Error */
}
extern char * f1(void)
{
return "ABC"; /* Message 0753 */
}
extern const char * f2(void)
{
return "XYZ"; /* */
}
MISRA C:2012 Rules applicable to message 0752:
| Rule-7.4 (Required) | A string literal shall not be assigned to an object unless the object's type is "pointer to const-qualified char" |
See also:
![]() | ||
| QA·C Source Code Analyser 8.1
MISRA C:2012 Compliance Module 1.0 © 2012 Programming Research. www.programmingresearch.com | Personality Groups | Glossary | Message Index | MISRA C:2012 Rule Index | Contents |