strlen

Hey , I just wanted to check the array size of a char .So I used strlen and I got the answer>

Then I made a char array , arr[2]; and I used it

cout<<"Insert :" ; cin>>arr;
cout<<"You inserted:" <<arr<endl ;

.The problem is I get "2" if i find the length of the array from strlen> But still .........it can save at least 20 characters.

So I cant explain it to my self How it happens .. Any one ?
Last edited on
I do not see how your character array defined as arr[2] can store at least 20 characters.
Then please tell me what should be the result of this .

#include<iostream>
#include<cstring>
using namespace std ;
int main()
{
char arr[3];
cout<<strlen(arr)<<endl ; // I got 2
cout<<"Insert :" ;
cin>>arr;
cout<<"you inserted: "<<arr<<endl ; // if i entered aaaaaaaaaaaaaa , I will get that prined. I thought that it will print up to 3 characters(aaa). Is my basic theory wrong ?
return 0 ;
}
Is my basic theory wrong ?


Yes. You have enough room for a c-string of length 2. (The 3rd element must be a nul character to terminate the c-string.)

On your first strlen: The contents of arr are unspecified. Using strlen here results in undefined behavior -- you know -- the stuff you want to avoid at all costs in a correct program.

You don't limit the number of chars you're extracting from the stream and cin has no way of knowing the size of the storage you're feeding to it, so it doesn't stop at 2 characters if you enter more than 2, so when you do enter more than 2 it results in undefined behavior -- you know -- the stuff you want to avoid at all costs in a correct program.

Your program could blow up your monitor and it would be within the range of undefined behavior. Don't write to or access memory you don't own.
Last edited on
closed account (4z0M4iN6)
strlen counts caracters until it finds the character with value 0 ('\0'). This has nothing to do with your size of the array. strlen starts at an address in the RAM and counts until it finds 0. 0 isn't included in the count.

Also strcpy, strcat, strcmp work this way, or printf, puts and many other functions more. The end of character strings always is marked by 0 or has to be marked by 0.
Last edited on
I will not repeat what was already said. I only will add that if you would use standard template class std::string you will get the result you were awaiting.:)
to get a input data with array, you can use a function from ctype.h
Example :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<iostream>
#include<ctype.h>

using namespace std ;

int main()
{
   char arr[6];
 
   cout<<"Insert :" ; 
   cin.getline(arr, sizeof(arr));  //a  function from library ctype.h
  
   cout<<"you inserted: "<<arr<<endl

   system("pause");
}


if i give a sentence whose length exceeds the length of the array, then the sentence will be printed along the size of the array that has been declared.

Example Output :

Insert : cplusplus

you inserted : cplus


May be useful.
@Hendro P Sinaga

Nothing in your code requires the inclusion of ctype.h, and had it been required in C++ the include is cctype.
@cire
ctype wasnt required, but still c++ supports all c librariesand u can use them as use in c along with c++ libraries.
@Cire
C++ support all C library.
For the sake of reminding people how to use cin >> with arrays, the original progam can be made valid with a simple setw():

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <cstring>
#include <iomanip>
using namespace std ;
int main()
{
    char arr[3] = {};
    cout << strlen(arr) << endl; // Now you get zero
    cout<< "Insert :" ;
    cin >> setw(3) >> arr;
    cout << "you inserted: " << arr << endl ; // try it
}
@anirudh sn and @Hendro P Sinaga
Let me show you this:
1
2
3
4
#include <stdio.h> // C Stdio header
#include <cstdio> // C++ Stdio header
#include <ctype.h> // C Ctype header
#include <cctype> // C++ Ctype header 

The C++-Corrected headers are like: "c" + (Name Of C Header) - ".h"
You'll see that, inside these C++-corrected headers, the C headers have been included. Just, DO NOT worry about that. Worry about the fact that you must include the C++-Corrected headers.
Topic archived. No new replies allowed.