bubbles
bubbles

Reputation: 881

How do I find the length of "char *" array in C?

I declare the following array:

char* array [2] = { "One", "Two"};

I pass this array to a function. How can I find the length of this array in the function?

Upvotes: 6

Views: 11730

Answers (6)

Trevor Hickey
Trevor Hickey

Reputation: 37914

Use the new C++11 std::array
http://www.cplusplus.com/reference/stl/array/
the standard array has the size method your looking for

Upvotes: 0

Joe McGrath
Joe McGrath

Reputation: 1501

If you mean how long are all the strings added togather.

int n=2;
int size=0;
char* array [n] = { "One", "Two"};

for (int i=0;i<n;++i)
  size += strlen(array[i];

Added:

yes thats what im currently doing but i wanted to remove that extra paramater. oh well –

Probably going to get a bad response for this, but you could always use the first pointer to store the size, as long as you don't deference it or mistake it for actually being a pointer.

char* array [] = { (char*)2,"One", "Two"};

long size=(long)array[0];
for(int i=1; i<= size;++i)
  printf("%s",array[i]);

Or you could NULL terminate your array

char* array [] = { "One", "Two", (char*)0 };

for(int i=0;array[i]!=0;++i)
{
  printf("%s",array[i]);
}

Upvotes: 0

sarnold
sarnold

Reputation: 104110

C is doing some trickery behind your back.

void foo(int array[]) {
    /* ... */
}

void bar(int *array) {
    /* ... */
}

Both of these are identical:

6.3.2.1.3: Except when it is the operand of the sizeof operator or the unary & operator, or is a string literal used to initialize an array, an expression that has type ‘‘array of type’’ is converted to an expression with type ‘‘pointer to type’’ that points to the initial element of the array object and is not an lvalue. If the array object has register storage class, the behavior is undefined.

As a result, you don't know, inside foo() or bar(), if you were called with an array, a portion of an array, or a pointer to a single integer:

int a[10];
int b[10];
int c;

foo(a);
foo(&b[1]);
foo(&c);

Some people like to write their functions like: void foo(int *array) just to remind themselves that they weren't really passed an array, but rather a pointer to an integer and there may or may not be more integers elsewhere nearby. Some people like to write their functions like: void foo(int array[]), to better remind themselves of what the function expects to be passed to it.

Regardless of which way you like to do it, if you want to know how long your array is, you've got a few options:

  1. Pass along a length paramenter too. (Think int main(int argc, char *argv)).
  2. Design your array so every element is non-NULL, except the last element. (Think char *s="almost a string"; or execve(2).)
  3. Design your function so it takes some other descriptor of the arguments. (Think printf("%s%i", "hello", 10); -- the string describes the other arguments. printf(3) uses stdarg(3) argument handling, but it could just as easily be an array.)

Upvotes: 2

kusma
kusma

Reputation: 6678

Getting the array-size from the pointer isn't possible. You could just terminate the array with a NULL-pointer. That way your function can search for the NULL-pointer to know the size, or simply just stop processing input once it hits the NULL...

Upvotes: 0

parapura rajkumar
parapura rajkumar

Reputation: 24433

When you pass an array there is NOT an easy way to determine the size within the function.

You can either pass the array size as a parameter or use std::vector<std::string>

If you are feeling particularly adventurous you can use some advanced template techniques

In a nutshell it looks something like

template <typename T, size_t N>
void YourFunction( T (&array)[N] )
{
    size_t myarraysize = N;
}

Upvotes: 7

Seth Carnegie
Seth Carnegie

Reputation: 75150

You can't find the length of an array after you pass it to a function without extra effort. You'll need to:

  1. Use a container that stores the size, such as vector (recommended).
  2. Pass the size along with it. This will probably require the least modification to your existing code and be the quickest fix.
  3. Use a sentinel value, like C strings do1. This makes finding the length of the array a linear time operation and if you forget the sentinel value your program will likely crash. This is the worst way to do it for most situations.
  4. Use templating to deduct the size of the array as you pass it. You can read about it here: How does this Array Size Template Work?

1 In case you were wondering, most people regret the fact that C strings work this way.

Upvotes: 17

Related Questions