Beginner Question - Size of Char Array

Apr 24, 2016 at 4:25pm
I need help determining the size of a char array. I get confused when asked "How many chars does this char array store?"

For example:

 
char bacon[6]


Allocates 6 spaces for storage, but does it hold 5 or 6 chars?
I've used strlen and sizeof, but I'm not sure which to refer to. Also, when I use strlen and size of on this code, I get two different values.

 
char pancakes[] = {'s','y','r','u','p'}

For sizeof I get 5, and strlen I get 19. I'm not sure where 19 is coming from.

Could someone please explain so I can understand?
Apr 24, 2016 at 4:56pm
In your first example, char bacon[ 6 ], that code declares an array that will hold up to six characters; however, if you want to use strlen() or any of the functions that work on "C" strings (null-terminated character arrays), it should hold at most five characters and a null byte.

BTW, the positions that are valid for your example are these:

char bacon[ 0 ][ 1 ][ 2 ][ 3 ] [ 4 ][ 5 ]

In your pancakes example, the compiler will create an array of five characters, since you initialized each position with a single character. The compiler doesn't add a null byte in this case.

I hope that helps.


Last edited on Apr 24, 2016 at 4:56pm
Apr 24, 2016 at 5:45pm
The size of the array does not change, but the size of the string it contains may vary, from zero to one less than the array size.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
    char word1[6] = {'\0'};                          // string of length zero.
    char word2[6] = {'A', '\0'};                     // string of length one.
    char word3[6] = {'D', 'o', 'g', '\0' };          // string of length three.
    char word4[6] = {'A', 'p', 'p', 'l', 'e', '\0'}; // Longest possible string, five letters and a null terminator.

    cout << '\"' << word1 << '\"'  << " has length of " << strlen(word1);
    cout << "  size is: " << sizeof(word1) << '\n';
          
    cout << '\"' << word2 << '\"'  << " has length of " << strlen(word2);
    cout << "  size is: " << sizeof(word2) << '\n';


    cout << '\"' << word3 << '\"'  << " has length of " << strlen(word3);
    cout << "  size is: " << sizeof(word3) << '\n';
    
    
    cout << '\"' << word4 << '\"'  << " has length of " << strlen(word4);
    cout << "  size is: " << sizeof(word4) << '\n';

Output:
"" has length of 0  size is: 6
"A" has length of 1  size is: 6
"Dog" has length of 3  size is: 6
"Apple" has length of 5  size is: 6


Often a character array will contain a string. But it doesn't necessarily have to. It may just contain individual characters.
1
2
3
4
    char array5[] = {'a', 'e', 'i', 'o', 'u'}; // This is NOT a string.
    for (int n=0; n<sizeof(array5); ++n)
        cout << array5[n];
    cout << "  size is: " << sizeof(array5) << '\n';

Output:
aeiou  size is: 5


Please see the tutorial for more details:
http://www.cplusplus.com/doc/tutorial/ntcs/

Apr 24, 2016 at 9:33pm
Thank you koothkeeper and Chervil, that helps tremendously.
Topic archived. No new replies allowed.