How can we determine the length of an array of strings when we don't know the length?
For example in this piece of code:
#include <stdio.h>
int main() {
int n;
char names[3][10] = { “Alex”, “Phillip”, “Collins” };
for (n = 0; n < 3; n++)
printf(“%s \n”, names[n]);
}
n < 3 is assuming you know the length of the array but how can you determine it's length when we don't know?
I have tried a few alternatives such as:
int arraySize() {
size_t size, i = 0;
int count = 0;
char names[3][10] = { “Alex”, “Phillip”, “Collins” };
size = sizeof(names) / sizeof(char*);
for (i = 0; i < size; i++) {
printf("%d - %s\n", count + 1, *(names + i));
count++;
}
printf("\n");
printf("The number of strings found are %d\n", count);
return 0;
}
or
for (n = 0; n < sizeof(names); n++)
but they all error out.
Any help is appreciated.
sizeof, what do you thinksizeof namesproduces? What doessizeof names[0]produce? What doessizeof names[0][0]produce? Do those give you any idea?