DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Jason has posted 22 posts at DZone. View Full User Profile

Read Multidimensional C Char Array Of Unknown Length

05.16.2007
| 16063 views |
  • submit to reddit
        Reading a multidimensional C char array (aka C String array) containing char 0 and terminated with literal zero without knowing length.

char *a[] = { "11", "23", "", "44", "11", "", "16", "36", "", "51", "71", "", "46", "26", "", "14", "68", 0};
int sentinel = 0;
int i = 0;
while ( *(a + i++) != sentinel ) { /* counting away */ }
int aLength = i; // 18, length
int aUbound = i - 1; // 17, index of last element
int aMaxIdx = i - 2; // 16, index of last element where a[i] doesn't cause seg fault
// You now have the lengths so read like normal