When a function passes parameters, the array needs to pass the array name and array length, but when passing a string, it does not need to pass the length. Why?
-
6When you say "string" do you mean like a C-style ***null-terminated*** string? Or a `std::string` object (which keeps track of the lenght)? Some example code in the form or a [mre] could help to show what you mean. – Some programmer dude Mar 15 '23 at 11:54
-
Because array often decays to pointers. Read [what is array to pointer decay](https://stackoverflow.com/questions/1461432/what-is-array-to-pointer-decay). Also `std::string` is a class-type and it stores **state information** inside the instances. – Jason Mar 15 '23 at 11:55
-
The implementation for `std::string` is ironically non-standard (there are variations between compilers), but overall the notion of passing a base pointer and a length usually still holds out. – Rogue Mar 15 '23 at 11:56
-
*"When a function passes parameters, the array needs to pass the array name and array length..."* This is not always true. Example, we can pass array by reference in which case there is no need to pass array length separately. – Jason Mar 15 '23 at 11:57
-
What I don't quite understand is that when passing a string (c type) as a parameter, there is no need to pass an additional length, because there is strlen function that can calculate the length in the function? – xiaohan Mar 15 '23 at 12:12
-
2C-style strings are terminated by a nul byte (and that's what `strlen` looks for). – Paul Sanders Mar 15 '23 at 12:15
-
[why char* passed to FUNCTION always with the len of the string](https://stackoverflow.com/questions/29879587/why-char-passed-to-function-always-with-the-len-of-the-string) – Jason Mar 15 '23 at 12:20
-
A C-style string have a special character that tells functions where the string ends. General non-string arrays have no such terminator value. If you only have a pointer to the first element of an array, then there's no way to tell where the array ends or what its size might be. – Some programmer dude Mar 15 '23 at 12:47
2 Answers
Because std::string
is a class-type and it stores(keeps track of) state information like length of the string. So there is no need to pass the length of the string separately here. The user can access the length of the string inside the called function using the std::string::size
member function.
On the other hand, arrays are built in types and do not store the length of a given array inside an array object. Thus when passing an array by value to a function, we need to separately pass the length of the array as argument.
Note that it is also possible to pass an array by reference, to a function and we can even create a function template that takes an arbitrary array by reference so that the caller will not have to explicitly pass the length of the array.

- 36,170
- 5
- 26
- 60
I suppose you mean the difference between passing a c-string, a null terminated string, and other arrays.
First of all, arrays decay to pointers to first element when passed to a function. So we are comparing:
void foo(char* s);
void bar(int* a, size_t size);
As the name, "null terminated string", suggests there is a \0
as last element in string. \0
is not a printable character, its purpose is to denote the last element of a string.
For arrays of other type there is no such convention. There is no value that would make an obvious choice as sentinel. If 0
would denote the last element in an integer array you would not be able to use integer arrays with a 0
in any other postition (well, you could but with major complications and inconvenience).
In c++ you almost never have to pass an array and size seperately. There is std::string
, std::vector
, and std::array
. Their size can be queried via size()
.

- 109,796
- 11
- 89
- 185