In c there is no “good” way to determine the size of an array. Because of this there are a lot of design patterns that either have statically size arrays or put some delimiter at the end to signal the end of the array (for example the null byte at the end of all strings).
So depending on your use case here it might make sense to pass an argc
parameter whenever you would want to call getSize
, or alternatively store a NULL at the end of the char**
and iterate over the array until you find it:
// snake case is generally standard for c
int get_size(char** args)
{
int i = 0;
// check that the first element of each arg is not ‘\0’
for ( ; args[i] != NULL; ++i) {
}
return i;
}
Without one of these approaches you are likely to encounter issues with accessing unallocated or freed memory, and there isn’t a good way to handle those, and any attempt to would certainly be less readable.