I need to specify the length of an array of char pointers as an input to a function in C. I don't think it is possible to know this in C without some trick (like iterating until a certain sentinel string is found), but I'm wondering if perhaps gcc has some function that would extract this for me?
const char *names[] = {"string1","next_string","test"};
//Later in code
int n_fields = <magic>(names);
In this case I would be looking for n_fields
to be 3.
My current alternative is to manually count and specify the length, but this means I need to ensure that these two values stay linked manually.
Edit regarding duplicate question: Unlike arrays of integers it was not obvious to me what the two sizeof parameters in other examples would be measuring for an array of char pointers. Specifically I thought the denominator might be measuring string length, when in reality it was measuring the size of a pointer. Pretty much every example online is for an array of ints, not char pointers.