0

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.

4
  • 1
    size_t array_size = sizeof str_table / sizeof str_table[0] Commented Mar 6, 2014 at 17:22
  • 1
    @IvanGrynko: That doesn't work if you've passed the array to a function Commented Mar 6, 2014 at 17:35
  • @MooingDuck of course, I mean that array_size should be passed as an argument to a function. Commented Mar 6, 2014 at 17:38
  • 1
    Yes, sizeof(str_table) / sizeof(str_table[0]) works, but when I pass the pointer, inside the function the same code does not work. Commented Mar 6, 2014 at 17:40

5 Answers 5

4

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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).

Comments

1

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.

2 Comments

Not inside of a function
OP wants to know how to get the size of the array after the array has been passed into a function, which cannot be done using your method.
0
(&str_table)[1] - str_table

2 Comments

Seems like an extremely roundabout way of achieving the same result as sizeof(str_table) / sizeof(str_table[0]).
@herohuyongtao str_table points to the beginning of the array and (&str_table)[1] to the end. see this
0

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);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.