Rule A2-13-4 (required, architecture / design / implementation,automated)

String literals shall not be assigned to non-constant pointers.

Rationale

Since C++0x, there was a change in subclause 2.13.5 for string literals. To prevent from calling an inappropriate function that might modify its argument, the type of a string literal was changed from “array of char” to “array of const char”. Such a usage is deprecated by the Standard and reported by a compiler as a warning. This rule is deliberately redundant, in case rules A1-1-1 and A1-4-3 are disabled in a project.

Example

//% $Id: A2-13-4.cpp 307578 2018-02-14 14:46:20Z michal.szczepankiewicz $

int main(void)
{

    //non-compliant
    char nc2[] = "AUTOSAR"; //compliant with A2-13-4, non-compliant with A18 -1-1

    char nc3[8] = "AUTOSAR"; //compliant with A2-13-4, non-compliant with A18 -1-1

    nc1[3] = ’a’; // undefined behaviour

    char* nc1 = "AUTOSAR";

    //compliant
    const char c2[] = "AUTOSAR"; //compliant with A2-13-4, non-compliant with A18-1-1

    const char c3[8] = "AUTOSAR"; //compliant with A2-13-4, non-compliant with A18-1-1

    //c1[3] = ’a’; //compilation error

    const char* c1 = "AUTOSAR";

    return 0;

}

See also

JSF December 2005 [8]: AV Rule 151.1: A string literal shall not be modified.