Take the 2-minute tour ×

I declare a static char array, then I pass it to a function. How to get the no. of bytes in the array inside the function?

share|improve this question
add comment

6 Answers

up vote 15 down vote accepted

You would have to pass it to the function. You can use sizeof() to get the size of an array.

const char foo[] = "foobar";

void doSomething( char *ptr, int length)


doSomething(foo, sizeof(foo));

This MSDN page has explains more about sizeof and has a bigger example.

Edit: * see j_random_hacker's answer for an intriguing technique using templates... *

share|improve this answer
Can I do it without passing? –  Ron Jan 17 '09 at 10:32
No. See my answer below. –  abelenky Jan 17 '09 at 10:35
Paul, your answer is correct for C, but in C++ you can define a function template that does "discover" the size of a static array. –  j_random_hacker Jan 17 '09 at 10:58
Nice! will reference it.. –  Paul Dixon Jan 17 '09 at 18:57
@brainfck: ... no. See msdn.microsoft.com/en-us/library/0w557fh7(VS.80).aspx for confirmation. –  int3 Nov 20 '09 at 14:11
show 1 more comment

Use a function template instead that has a non-type template parameter:

template <size_t N>
void func(char (&a)[N]) {
        cout << "a[" << i << "] = " << a[i] << endl;   // Or whatever you want to do

To call:

char myArray[500];        // Or "static char myArray[500]", if you want

A new copy of this function will be instantiated for each distinct size of array that it is called with, so if you call it with many different-sized arrays, you'll get some code bloat. But that's not likely to be the case.

share|improve this answer
What a great idea! thanks. –  Oren S May 25 '10 at 10:35
Nice! template<typename T, size_t N> size_t elments_of_array( T (&arr)[N] ) { return N; } –  fmuecke Jul 16 '10 at 9:09
@fmuecke: In that case better go compile-time - see this question. –  Georg Fritzsche Nov 19 '10 at 3:29
add comment

No. Don't use arrays. Use a vector instead. These days there is almost no excuse for using arrays because they are unsafe. AFAIK, they are one of the main reasons for software problems because it's so easy to accidently overrun the end of the array.

Using a vector, you don't have to worry any more about buffer overruns. And your function can easily find out the size of the vecor.

#include <vector>
vector<char> myVector;

void DoSomething(vector<char> &v)
    int sizeOfVector = v.size();


share|improve this answer
add comment

You can't. Arrays in C++ are pointers, and that is all you have: the pointer to the beginning of the array. If it happens to be a string, you can use strlen to measure its length. If its some other known format, you can calculate the length according to that format.

Consider this code:

static char str[] = "hello world";


void foo(char* str)
   // length of str is unknown

void bar(char str[])
  // length of str is still unknown

Regardless of if your function parameter is a char[] or a char*, you don't know the size.

I suggest passing the size in as a separate parameter.

share|improve this answer
add comment

One other option you have is creating a string class that manages the string. I'm sure someone out there has done this already. The most primitive version is a struct with a char * and the buffer length, where you have to manually manage the size whenever it changes. The other end of the spectrum is a fully implemented string class, with operator overloading and manupulation functions.

Then pass this class to your function and it already knows the size. It isn't really any different then passing it as a separate parameter; it simply is an easier way of managing strings if they're all different lengths.

share|improve this answer
You're on the right track, but why invent your own string class when std::string already exists and does exactly what is needed? –  j_random_hacker Jan 17 '09 at 11:00
Also true. It's been a while... –  lc. Jan 17 '09 at 12:33
add comment

int array_size = sizeof(Array) / sizeof(Array[0]);

share|improve this answer
this is very error prone, as someone might replace the array with a pointer, causing the size to be incorrect. I would use j_random_hacker's method as it refuses to compile in these situations. –  Oren S May 25 '10 at 10:39
add comment

Your Answer


