Why in Character Arrays case the `Size() , Length(), strlen()` does not work in C++?

Viewed 50

I am trying to find the length of the string which I am entering in the char array of size 10 using size(), strlen(),length() functions in c++ but they are not working why??? but instead of that char array if I would define a string ="Vishal " then use these functions are then they are working Why so???. And it is working in Case of the strings.

#include <bits/stdc++.h>
using namespace std;
int main()
{

    char name[10];
    cin >> name;
    int count = 0;
    for (int i = 0; i < name.size(); i++)
    {
        count++;
    }

    cout << count << "\n";
    return 0;
}
1 Answers

C++ use of arrays is inherited from C. C is not an object-oriented language and so doesn't have method calls (like .size()).

C++ decided to keep arrays from C for backwards compatibility reasons but in new C++ code you should almost always prefer the C++ equivalents such as std::string, std::vector and std::array.

Related