Never been to DZone Snippets before?

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

Read Multidimensional C char Array of Unknown Length (See related posts)

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

You need to create an account or log in to post comments to this site.


Click here to browse all 4858 code snippets

Related Posts