fork download
  1. #include <stddef.h>
  2. #include <stdio.h>
  3.  
  4. #define countof(array) (sizeof(array) / sizeof((array)[0]))
  5.  
  6. static const char *const a[] = { "One", "Two", "Three" };
  7. static const char *const b[] = { "One", "Two", "Three", "Four", "Five" };
  8.  
  9. static const struct { const char *const *items; size_t length; } ab[] =
  10. {
  11. { a, countof(a) },
  12. { b, countof(b) },
  13. };
  14.  
  15. static const char *const c[] = { "One", "Two", "Three", NULL };
  16. static const char *const d[] = { "One", "Two", "Three", "Four", "Five", NULL };
  17. static const char *const *const cd[] = { c, d, NULL };
  18.  
  19. int main(void)
  20. {
  21. puts("Using hardcoded lengths:");
  22. for (size_t i = 0; i < countof(ab); i++) {
  23. printf("%zu:\n", i);
  24. for (size_t j = 0; j < ab[i].length; j++) {
  25. printf("\t%zu: %s\n", j, ab[i].items[j]);
  26. }
  27. }
  28. puts("Using NULL-terminated arrays:");
  29. for (const char *const *const *p = cd; *p; p++) {
  30. puts("-");
  31. for (const char *const *q = *p; *q; q++) {
  32. printf("\t%s\n", *q);
  33. }
  34. }
  35. }
  36.  
Success #stdin #stdout 0s 9424KB
stdin
Standard input is empty
stdout
Using hardcoded lengths:
0:
	0: One
	1: Two
	2: Three
1:
	0: One
	1: Two
	2: Three
	3: Four
	4: Five
Using NULL-terminated arrays:
-
	One
	Two
	Three
-
	One
	Two
	Three
	Four
	Five