How to find the size of a char array

I want to have a char array and work with every letter separately. How do i check how many letters a char array has?
I thought sizeof(name)/sizeof(name[0]) would work but it doesnt. For loop where i != '\0' is the same story
Use a std::string and it will simply tell you.

Failing that, show your code.
I thought sizeof(name)/sizeof(name[0]) would work but it doesnt.
That tells you how many fields an array has.

For loop where i != '\0' is the same story
No, it's not the same story. You need of course place '\0' at the end of the string.

Note that "abc" has a '\0' at the end of the string.

How do i check how many letters a char array has?
If you do not want the additional '\0' you should consider another variable that maintains the size of the contained data.

In c++ you have container that will do the work for you though.
For a char array, use std::strlen() - include <cstring>
you can store the length in the first location or two if you want to store it without wrapping it up in an object or having tag-alongs parameters in every function.
eg arr[0] = 3; sprintf(&arr[1], "abc"); //usually good enough, 256 max size treat [0] as unsigned
or if you need gigantic cast to a short, int, 64 bit int, whatever and use the first 2,4,8 bytes as size.
if you want to be a smart* ... c++ supports negative array index, so you can pointer to the string and back up to the size with some smoke and mirrors.
are you allowed to use strlen?
Last edited on
sizeof(array)/sizeof(array[0])

_countof(array)

These tell you the size (capacity) of the array, i.e. how much storage space there is in the array. They do not tell you anything about the current content of the array! Specifically, they do not tell you the "length" of the string, i.e. how many non-NULL characters there are in the array before the terminating NULL character.

Also, the above code only works with an actual array, it does not work with plain char* pointers at all !!!


If you want to determine the "length" of a string, not counting the final NULL character, use strlen() function:

https://www.cplusplus.com/reference/cstring/strlen/

Warning: When using strlen() there must be a terminating NULL character in the string/array !!!


BTW: sizeof() and _countof() are evaluated at compile-time, whereas strlen() is evaluated at runtime.
Last edited on
> I want to have a char array and work with every letter separately.
> How do i check how many letters a char array has?

Use std::span<char> https://en.cppreference.com/w/cpp/container/span
if the characters need to be modified;

or std::string_view https://en.cppreference.com/w/cpp/string/basic_string_view
if that is not a requirement.

For example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <string_view>
#include <span>
#include <cctype>

void fancy_print( std::string_view str )
{
    std::cout << '"' ;
    for( char c : str ) std::cout << c << '-' ;
    std::cout << "\"  [" << str.size() << " chars]\n" ;
}

void to_upper( std::span<char> str )
{
    for( char& c : str ) c = std::toupper( static_cast<unsigned char>(c) ) ;
}


int main()
{
    char cstr[1000] = "aBcD.123.eFgH" ;
    fancy_print(cstr) ;

    to_upper(cstr) ;
    fancy_print(cstr) ;
}

http://coliru.stacked-crooked.com/a/795722ad0e77511a
Topic archived. No new replies allowed.