Synopsis: | Don't use sizeof on a variable of an array type that is passed as argument. |
Language: | C |
Severity Level: | 2 |
Category: | Expressions |
Description: |
Justification Variables of array types are treated as pointers when passed as arguments. As a result the sizeof operator will return the size of the array type instead of the size of the array. This is not intuitive and can easily lead to mistakes. Example void m1(char a[100]) { int x = sizeof(a); /* WRONG, x == 4 or x == 8 */ } void m2(void) { char a[100] = { 0 }; int x = sizeof(a); /* RIGHT, x == 100 */ } |