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

前端 未结 9 1380
长发绾君心
长发绾君心 2020-12-13 18:00
char* names[]={\"A\", \"B\", \"C\"};

Is there a way to find the number of strings in the array. Like, for example in this case, it has to be 3. Ple

相关标签:
9条回答
  • 2020-12-13 18:44

    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 myArray
    
    printf("%i", myArray.length);
    destroy(myArray);
    
    0 讨论(0)
  • 2020-12-13 18:44

    Making the Array Of Strings to run in a While loop with an Increment Operator, Until the loop becomes NULL, gives the Array Size:

    #include<stdio.h>
    char* names[]={"A", "B", "C"};
    
    int main(){
    int nameSize = 0;
    while(names[++nameSize]!='\0');
    }
    
    for(i=0;i<nameSize;i++){
    printf("%s\n", names[i]);
    }
    
    return 0;
    }
    

    Output:

    A
    B
    C
    
    0 讨论(0)
  • 2020-12-13 18:46

    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 = 3
    len 2 = 3
    len 3 = 4
    len 4 = 4
    len 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.

    0 讨论(0)
提交回复
热议问题