user34920
user34920

Reputation: 247

size of array of pointers to strings?

I have these strings and an array of pointers to these strings defined like so (in C):

char str0[]= "Hello World";
char str1[]= "Byebye Baby";
char str2[]= "Regtime Girl";
char str3[]= "My Darling";

char *str_table[] = {str0,str1,str2,str3};

I need to pass *str_table[] (array of pointers) to a function that will scan the various strings for all kind of things. The question is how can I find out the number of pointers in the array? Seems like sizeof() doesn't work as I thought it would.

Upvotes: 0

Views: 157

Answers (5)

David Heffernan
David Heffernan

Reputation: 612794

You can only recover this information if you have the str_table variable available. In that case you can write:

sizeof(str_table) / sizeof(str_table[0])

If you are trying to work out the length in a function that receives the array as a parameter, it is too late. At that point you cannot recover the information. If you are in that position, you will have to pass the length to the function, as well as the array.

The function might look like this:

void scan(const char* arr[], const size_t len)
{
    for (size_t i=0; i<len; i++)
        doSomething(arr[i]);
}

And you call it like this:

size_t len = sizeof(str_table) / sizeof(str_table[0]);
scan(str_table, len);

From inside the function, arr is a pointer of type const char**. No length information is provided.

Another approach to this problem is to null-terminate the array. Like this:

const char *str_table[] = { str0, str1, str2, str3, NULL };

This is exactly the same idea as is used with C strings. You can pass just the array to a function, and the function can iterate over the array until it encounters a null pointer.

Upvotes: 4

alk
alk

Reputation: 70893

To find the number of string pointer inside a function which got the array passed in: Terminated the array, like this:

char *str_table[] = {str0,str1,str2,str3, NULL};

Add a function check the size like this:

ssize_t elements_in_terminated_array(char ** pp)
{
  ssize_t s = -1;

  if (NULL != pp)
  {
    char ** tmp = pp;
    while(*tmp)
    {
      ++tmp;
    }

    s =  tmp - pp;
  }

  return s;
}

int main(void)
{
  ssize_t s = elements_in_terminated_array(str_table);
}

Upvotes: 0

haccks
haccks

Reputation: 105992

You need sizeof(str_table) / sizeof(str_table[0]). sizeof(str_table) will give you the size of entire array (in bytes). Dividing it with the size of array element will give you the number of elements in the array.
Now you also need to pass this size of array to your function along with other arguments.

Upvotes: 1

TripShock
TripShock

Reputation: 4249

What you are experiencing is what is referred to as "array decay". See this question for details on that. Basically, when an array is passed to a function, you can no longer do a sizeof on it because it has "decayed" to a pointer, and sizeof will just return the size of the pointer. You will have to calculate the size of the array while it is still an array and pass it into the function.

The answer to the question linked above also shows you another option if the array size is fixed at compile time (see the pass-by-reference method).

Upvotes: 1

Arjun Sreedharan
Arjun Sreedharan

Reputation: 11453

(&str_table)[1] - str_table

Upvotes: 0

Related Questions