 
                            ...
| Code Block | ||||
|---|---|---|---|---|
| 
 | ||||
| void clear(int array[]) {
  for (size_t i = 0; i < sizeof(array) / sizeof(array[0]); ++i) {
     array[i] = 0;
   }
}
void dowork(void) {
  int dis[12];
  clear(dis);
  /* ... */
}
 | 
Footnote 103 in Section section 6.5.3.4 of the C Standard [ISO/IEC 9899:2011] explains:
...
In this compliant solution, the size of the array is determined inside the block in which it is declared and passed as an argument to the function.:
| Code Block | ||||
|---|---|---|---|---|
| 
 | ||||
| void clear(int array[], size_t len) {
    for (size_t i = 0; i < len; i++) {
     array[i] = 0;
  }
}
void dowork(void) {
  int dis[12];
  clear(dis, sizeof(dis) / sizeof(dis[0]));
  /* ... */
}
 | 
...
In this noncompliant code example, the sizeof a does not equal 100 * sizeof(int) because the sizeof operator, when applied to a parameter declared to have array or function type, yields the size of the adjusted (pointer) type, even if the parameter declaration specifies a length.:
| Code Block | ||||
|---|---|---|---|---|
| 
 | ||||
| enum {ARR_LEN = 100};
void clear(int a[ARR_LEN]) {
  memset(a, 0, sizeof(a)); /* error */
}
int main(void) {
  int b[ARR_LEN];
  clear(b);
  assert(b[ARR_LEN / 2]==0); /* may fail */
  return 0;
}
 | 
...
In this compliant solution, the size is specified using the expression len * sizeof(int).:
| Code Block | ||||
|---|---|---|---|---|
| 
 | ||||
| enum {ARR_LEN = 100};
void clear(int a[], size_t len) {
  memset(a, 0, len * sizeof(int));
}
int main(void) {
  int b[ARR_LEN];
  clear(b, ARR_LEN);
  assert(b[ARR_LEN / 2]==0); /* cannot fail */
  return 0;
}
 | 
...
| Tool | Version | Checker | Description | ||||||
|---|---|---|---|---|---|---|---|---|---|
| Compass/ROSE | 
 | 
 | Can detect violations of the recommendation but cannot distinguish between incomplete array declarations and pointer declarations. | ||||||
| 
 | 401 S | Partially implemented. | |||||||
| Splint | 
 | 
 | 
 | 
...