There are 2 ways in C++ to get the size of an array. The first is using run-time computation with sizeof, but there is a better compile-time way to compute the size. The way to do it as follows:
Note: char* is not the same as char[]. When you do sizeof(char *) it will give you 4 or 8 depending on the size of char in the system.
1 Comment
Alex · July 4, 2025 at 6:54 am
sizeof is compile time function.
“`
char a[] = “1234”;
constexpr auto size_a = sizeof(a);
// 4 + nullterminator.
static_assert(size_a == 5);
“`