Main Content

MISRA C:2023 Rule 17.6

The declaration of an array parameter shall not contain the static keyword between the [ ]

Since R2024a

Description

Rule Definition

The declaration of an array parameter shall not contain the static keyword between the [ ].

Rationale

If you use the static keyword within [] for an array parameter of a function, you can inform a C99 compiler that the array contains a minimum number of elements. The compiler can use this information to generate efficient code for certain processors. However, in your function call, if you provide less than the specified minimum number, the behavior is not defined.

Troubleshooting

If you expect a rule violation but do not see it, refer to Diagnose Why Coding Standard Violations Do Not Appear as Expected.

Examples

expand all

extern int arr1[20];
extern int arr2[10];


unsigned int total (unsigned int n,
                    unsigned int arr[static 20]) { // Non-compliant
 
    unsigned int i;
    unsigned int sum = 0;

    for (i=0U; i < n; i++) {
        sum+= arr[i];
    }

    return sum;
}

void func (void) {
    int res, res2;
    res = total (10U, arr1); //Undefined behavior 
    res2 = total (20U, arr2); 
}

In this example, the rule is violated when the static keyword is used within [] in the array parameter of function total. Even if you call total with array arguments where the behavior is well-defined, the rule violation occurs.

Check Information

Group: Function
Category: Mandatory
AGC Category: Mandatory

Version History

Introduced in R2024a