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?
7 Answers
Use a function template instead that has a non-type template parameter:
template <size_t N>
void func(char (&a)[N]) {
for (int i = 0; i < N; ++i) {
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
func(myArray);
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.

- 50,331
- 10
- 105
- 169
-
1Nice! template
size_t elments_of_array( T (&arr)[N] ) { return N; } – fmuecke Jul 16 '10 at 09:09 -
2@fmuecke: In that case better go compile-time - see [this question](http://stackoverflow.com/q/1500363/168225). – Georg Fritzsche Nov 19 '10 at 03:29
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... *

- 1
- 1

- 295,876
- 54
- 310
- 348
-
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
-
You should divide the length through 8! sizeof() returns the number of bits - not bytes. – brainfck Nov 20 '09 at 13:44
-
3@brainfck: ... no. See http://msdn.microsoft.com/en-us/library/0w557fh7(VS.80).aspx for confirmation. – int3 Nov 20 '09 at 14:11
-
There shouldn't be parenthesis in this usage. `sizeof(type) = for types ; sizeof expression = for variables` – Sandburg Oct 28 '21 at 08:27
You can also use std::size()
from C++17
https://en.cppreference.com/w/cpp/iterator/size
#include <iostream>
#include <vector>
#include <iterator>
int main()
{
std::vector<int> v = { 3, 1, 4 };
std::cout << std::size(v) << '\n';
int a[] = { -5, 10, 15 };
std::cout << std::size(a) << '\n';
}
For pre c++17 copy sample implementations ;)
template <class T, std::size_t N>
constexpr std::size_t size(const T (&array)[N]) noexcept
{
return N;
}

- 11,187
- 7
- 57
- 68
int array_size = sizeof(Array) / sizeof(Array[0]);
-
2this 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
-
This is an absolutely horrible macro. Somebody had this as a macro, then it was refactored from an array to a vector
, and everything just broke. Only symptom was silent overwriting of memory, followed by a crash hours later. At the very least, add a static assert before uses of it to catch this sort of bug. – Contango May 03 '20 at 09:36
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";
foo(str);
bar(str);
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.

- 63,815
- 23
- 109
- 159
-
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:57
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();
}

- 5,656
- 8
- 36
- 47
-
2"it's so easy to accidently overrun the end of the array." This is no different than for a vector. – Cris Luengo Jan 14 '21 at 18:14
-
@CrisLuengo - That's true, but at least vectors give you: A) access to .size(), and B) auto iteration. With an array, you're relying on the programmer to implement some external mechanism to keep track of the size of the array, and this can easily be broken. – Rocketmagnet Jun 08 '21 at 12:49
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.

- 113,939
- 20
- 158
- 187
-
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