Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get length of string array of unknown length

Tags:

arrays

c

pointers

I have this function:

int setIncludes(char *includes[]);

I don't know how many values includes will take. It may take includes[5], it may take includes[500]. So what function could I use to get the length of includes?

like image 263
Mohit Deshpande Avatar asked Sep 24 '10 01:09

Mohit Deshpande


People also ask

How do you find the length of a string array?

With the help of the length variable, we can obtain the size of the array. string. length() : length() method is a final variable which is applicable for string objects. The length() method returns the number of characters present in the string.

Can you find length from array?

The simplest procedural way to get the value of the length of an array is by using the sizeof operator. First you need to determine the size of the array. Then you need to divide it by the size of one element. It works because every item in the array has the same type, and as such the same size.

How do I get the length of a string array in C++?

3. Using sizeof() function to Find Array Length in C++ The sizeof() operator in C++ returns the size of the passed variable or data in bytes.

How do you find the length of an array in a for loop?

As already mentioned, you can iterate through an array using the length attribute. The loop for this will iterate through all the elements one by one till (length-1) the element is reached (since arrays start from 0). Using this loop you can search if a specific value is present in the array or not.


3 Answers

There is none. That's because arrays will decay to a pointer to the first element when passing to a function.

You have to either pass the length yourself or use something in the array itself to indicate the size.


First, the "pass the length" option. Call your function with something like:

int setIncludes (char *includes[], size_t count) {
    // Length is count.
}
:
char *arr[] = {"Hello,", "my", "name", "is", "Pax."};
setIncludes (arr, sizeof (arr) / sizeof (*arr));
setIncludes (arr, 2); // if you don't want to process them all.

A sentinel method uses a special value at the end to indicate no more elements (similar to the \0 at the end of a C char array to indicate a string) and would be something like this:

int setIncludes (char *includes[]) {
    size_t count = 0;
    while (includes[count] != NULL) count++;
    // Length is count.
}
:
char *arr[] = {"Hello,", "my", "name", "is", "Pax.", NULL};
setIncludes (arr);

Another method I've seen used (mostly for integral arrays) is to use the first item as a length (similar to Rexx stem variables):

int setIncludes (int includes[]) {
    // Length is includes[0].
    // Only process includes[1] thru includes[includes[0]-1].
}
:
int arr[] = {4,11,22,33,44};
setIncludes (arr);
like image 177
paxdiablo Avatar answered Oct 13 '22 09:10

paxdiablo


You have two options:

  1. You can include a second parameter, similar to:

    int main(int argc, char**argv)

  2. ... or you can double-null terminate the list:

    char* items[] = { "one", "two", "three", NULL }

like image 2
sukru Avatar answered Oct 13 '22 08:10

sukru


There is no way to simply determine the size of an arbitrary array like this in C. It requires runtime information that is not provided in a standard way.

The best way to support this is to take in the length of the array in the function as another parameter.

like image 1
JaredPar Avatar answered Oct 13 '22 10:10

JaredPar