Over a million developers have joined DZone.

Read Multidimensional C Char Array Of Unknown Length

·
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
Topics:

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}