Search⌘ K
AI Features

Discussion: Sizing Up Some Characters

Explore how the sizeof operator works with character arrays and pointers in C++. Understand the difference between array and pointer types, how to accurately count array elements, and why the sizeof operator behaves differently in function parameters. This lesson will help you write safer, clearer C++ code by using modern containers and recognizing potential pitfalls related to array size and pointer conversions.

Run the code

...
C++
#include <iostream>
void serialize(char characters[])
{
std::cout << sizeof(characters) << "\n";
}
int main()
{
char characters[] = {'a', 'b', 'c'};
std::cout << sizeof(characters) << "\n";
std::cout << sizeof(characters) / sizeof(characters[0]) << "\n";
serialize(characters);
}

Understanding the output

...