C and C++


LANG.CAST.ARRAY.POINTER : Array to Pointer Decay

Summary

An identifier with array type is passed as a function argument and decays to a pointer.

See also Array Parameter.

Properties

Class Name Array to Pointer Decay
Significance style
Mnemonic LANG.CAST.ARRAY.POINTER
Categories
AUTOSARC++14 AUTOSARC++14:M5-2-12 An identifier with array type passed as a function argument shall not decay to a pointer.
  AUTOSARC++14:A18-1-1 C-style arrays shall not be used.
MisraC++2008 MisraC++2008:5-2-12 An identifier with array type passed as a function argument shall not decay to a pointer.
MisraC++2023 MisraC++2023:7.11.2 An array passed as a function argument shall not decay to a pointer
JSF++ JSF++:183 Every possible measure should be taken to avoid type casting.
Availability Available for C and C++.
Enabling Checks for this warning class are disabled by default, and require the unnormalized C ASTs for the project. To enable them, add the following WARNING_FILTER rule and RETAIN_UNNORMALIZED_C_AST specification to the project configuration file.
RETAIN_UNNORMALIZED_C_AST = Yes
WARNING_FILTER += allow class="Array to Pointer Decay"
Note that retaining the unnormalized ASTs will increase the disk space used to store the project representation, and may make the analysis take longer.

Example

int ptr_arg(int *p);
int no_dimensionality(int a[5]);
int dimensionality_preserved(int (&a)[5]);

int lang_cast_array_pointer() {
   int retval=0;
   int myarr[5];

   retval += ptr_arg(myarr);                  /* 'Array to Pointer Decay' warning issued here */
   retval += no_dimensionality(myarr);        /* 'Array to Pointer Decay' warning issued here */
   retval += dimensionality_preserved(myarr);                       /* ok: myarr does not decay to a pointer */

   return retval;
}

Relevant Configuration File Parameters

The following configuration file parameters affect checks for this warning class.