How to find size or length of char array [duplicate]

I have a char array declared as char input[], and I want to know how to find its size or length. What is the correct syntax or method in C++ to determine the length or size of the char array?

Could you please provide code examples

New contributor

Nilesh Kudale is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

8

Where the name of an array is in scope, you can use sizeof to find the size of that array.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>char foo[1234];
std::cout << sizeof(foo); // should print 1234
</code>
<code>char foo[1234]; std::cout << sizeof(foo); // should print 1234 </code>
char foo[1234];

std::cout << sizeof(foo); // should print 1234

There is, however, a twist to this: when/if you attempt to define a function parameter with a type array of T, the parameter will actually have the type pointer to T. So, if you have something like this:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>void printSize(char param[]) {
std::cout << sizeof(param) << "n";
}
int main() {
char foo[1234];
char bar[2345];
printSize(foo);
printSize(bar);
}
</code>
<code>void printSize(char param[]) { std::cout << sizeof(param) << "n"; } int main() { char foo[1234]; char bar[2345]; printSize(foo); printSize(bar); } </code>
void printSize(char param[]) {
    std::cout << sizeof(param) << "n";
}

int main() { 
    char foo[1234];
    char bar[2345];

    printSize(foo);
    printSize(bar);
}

On a reasonably current compiler, this will generally print out 8 for both sizes. On an older compiler, you might get 4 for both. If you get a really old (MS-DOS) compiler, you could even get 2 for both. But always the same size for both, and virtually always 2, 4, or 8.

It’s also possible to pass the array to a function template by reference, in which case you can maintain the information about the array’s size:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>template <std::size_t N>
void printSize(char (&array)[N]) {
std::cout << N << "n";
}
int main() {
char foo[1234];
char bar[2345];
printSize(foo);
printSize(bar);
}
</code>
<code>template <std::size_t N> void printSize(char (&array)[N]) { std::cout << N << "n"; } int main() { char foo[1234]; char bar[2345]; printSize(foo); printSize(bar); } </code>
template <std::size_t N>
void printSize(char (&array)[N]) {
    std::cout << N << "n";
}

int main() { 
    char foo[1234];
    char bar[2345];

    printSize(foo);
    printSize(bar);
}

This should print the sizes as 1234 and 2345. And since printSize is defined to receive an array by reference, if you try to pass something like a pointer instead of an array, the code simply won’t compile.

Since it’s a template anyway, it’s easy to make this work for arrays of different types, not just arrays of char:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>template <typename T, std::size_t N>
void printSize(T (&array)[N]) {
std::cout << N << "n";
}
int main() {
char foo[1234];
long bar[2345];
printSize(foo);
printSize(bar);
}
</code>
<code>template <typename T, std::size_t N> void printSize(T (&array)[N]) { std::cout << N << "n"; } int main() { char foo[1234]; long bar[2345]; printSize(foo); printSize(bar); } </code>
template <typename T, std::size_t N>
void printSize(T (&array)[N]) {
    std::cout << N << "n";
}

int main() { 
    char foo[1234];
    long bar[2345];

    printSize(foo);
    printSize(bar);
}

This can produce different results from the first code above though. sizeof yields the number of bytes in an array, but this prints out the number of elements in the array. For any type that isn’t one byte, the results will be different (e.g., an array of 1000 shorts will have 1000 elements, but since short is usually two bytes, the sizeof version will show its size as 2000 instead).

Though it’s one most would generally advise avoiding, there’s one other possibility I suppose I need to mention. You can define a global variable in one file, and a declare it in another. The definition needs to specify the size, but the declaration does not. If the declaration doesn’t specify the size, that variable has an incomplete type in the file where it’s declared, so even though you can use it in some ways, you can’t take its size (in that file). To allow sizeof to work on it, you have to specify its size in the declaration.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>extern char foo[]; // legal, but incomplete type
extern char bar[1234]; // legal, complete type
</code>
<code>extern char foo[]; // legal, but incomplete type extern char bar[1234]; // legal, complete type </code>
extern char foo[]; // legal, but incomplete type
extern char bar[1234]; // legal, complete type

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật