Example 1 — Zero-filled counters
Count letters without initializing the array manually.
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int *count = calloc(3, sizeof *count);
if (!count) return 1;
const char *word = "abca";
for (const char *p = word; *p; p++) {
if (*p >= 'a' && *p <= 'c') count[*p - 'a']++;
}
printf("a=%d b=%d c=%d\n", count[0], count[1], count[2]);
free(count);
return 0;
} How It Works
callocstarts every counter at 0.*p - 'a'converts a letter to index 0..2.- Finally the array is freed.
