Synopsis: | A variable declared as an array shall not be used as a pointer. |
Language: | C |
Severity Level: | 6 |
Category: | Expressions |
Description: |
Justification An array name acts as a pointer to the first element of the array. However, using it that way can be a source of programming errors if the programmer forgets to process other elements of the array. Example 1 { int a[10]; *a = 1; /* WRONG */ a[0] = 1; /* RIGHT */ } Example 2 typedef struct { int x; int y; } ccbb_struct_t; static void ccbb_f(void) { ccbb_struct_t a[3]; a->x = 1; /* WRONG */ a[0].x = 1; /* RIGHT */ } Note An array name can be assigned to a pointer, making it a pointer to an array element. In this case, the programmer's intention is clear. Example 3 { int a[10]; int *a_p; *a = 1; /* WRONG */ a_p = a; *a_p = 1; /* RIGHT */ } |