Find the number of strings in an array of strings in C Find the number of strings in an array of strings in C arrays arrays

Find the number of strings in an array of strings in C


In this case you can divide the total size by the size of the first element:

num = sizeof(names) / sizeof(names[0]);

Careful though, this works with arrays. It won't work with pointers.


For an array, which the examples in the bounty are, doing sizeof(names)/sizeof(names[0]) is sufficient to determine the length of the array.

The fact that the strings in the examples are of different length does not matter. names is an array of char *, so the total size of the array in bytes is the number of elements in array times the size of each element (i.e. a char *). Each of those pointers could point to a string of any length, or to NULL. Doesn't matter.

Test program:

#include<stdio.h>int main(void){    char* names1[]={"A", "B", "C"}; // Three elements    char* names2[]={"A", "", "C"}; // Three elements    char* names3[]={"", "A", "C", ""}; // Four elements    char* names4[]={"John", "Paul", "George", "Ringo"}; // Four elements    char* names5[]={"", "B", NULL, NULL, "E"}; // Five elements    printf("len 1 = %zu\n",sizeof(names1)/sizeof(names1[0]));    printf("len 2 = %zu\n",sizeof(names2)/sizeof(names2[0]));    printf("len 3 = %zu\n",sizeof(names3)/sizeof(names3[0]));    printf("len 4 = %zu\n",sizeof(names4)/sizeof(names4[0]));    printf("len 5 = %zu\n",sizeof(names5)/sizeof(names5[0]));}

Output:

len 1 = 3len 2 = 3len 3 = 4len 4 = 4len 5 = 5

EDIT:

To clarify, this only works if you've defined an array, i.e. char *names[] or char names[][], and you're in the scope where the array was defined. If it's defined as char **names then you have a pointer which functions as an array and the above technique won't work. Similarly if char *names[] is a function parameter, in which case the array decays to the address of the first element.


It depends on how your array is created. In C, there is no way to check the length of an array that is a pointer, unless it has a sentinel element, or an integer passed with the array as a count of the elements in the array. In your case, you could use this:

int namesLen = sizeof(names) / sizeof(char);

However, if your array is a pointer,

char **names = { "A", "B", "C" };

You can either have an integer that is constant for the length of the array:

int namesLen = 3;

Or add a sentinel value (e.g. NULL)

char **names = { "A", "B", "C", NULL };int namesLen = -1;while (names[++namesLen] != NULL) { /* do nothing */}// namesLen is now the length of your array

As another way, you could create a struct that is filled with the values you want:

struct Array {    void **values;    int length;};#define array(elements...) ({ void *values[] = { elements }; array_make(values, sizeof(values) / sizeof(void *)); })#define destroy(arr) ({ free(arr.values); })struct Array array_make(void **elements, int count){    struct Array ret;    ret.values = malloc(sizeof(void *) * count);    ret.length = count;    for (int i = 0; i < count; i++) {        ret.values[i] = elements[i];    }    return ret;}

And you would use it as such:

struct Array myArray = array("Hi", "There", "This", "Is", "A", "Test");// use myArrayprintf("%i", myArray.length);destroy(myArray);