c++ - Strange behaviour of strlen -


I created a program in C ++ in which I used two four arrays that I started at the time of the declaration And when I got the Strlen () function to calculate their length, I found the odd output code shown below.

  # include & lt; Stdio.h & gt; #to & lt include, string.h & gt; #include & lt; string & gt; using namespace std; All four consonants [] = {'B', 'C', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N' P ',' q ',' r ',' s', 't', 'v', 'w', 'x', 'y', 'z'}; Four vowels [] = {'A', 'E', 'I', 'O', 'U'}; Int main () {int lenv, lenc; Lenc = strlen (dishes); Lane = Stellen (vowel); Printf ("lenv =% d and lenc =% d \ n", lenv, lenc); Return 0; }   

When running on ideone for the above program

  lenv = 26 and lenc = 21   

And when the codeblocks run on windows

  lenv = 5 and lenc = 26   

Please tell me the reason for such strange behavior. .

There is no strange behavior here, your strings are not eliminated, hence the strlen There is no way for the () function, to identify where they are.

When you start your string like this:

  four dishes [] = {'B', 'C', 'D', 'F' , 'J', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's',' t ',' V ',' w ',' x ',' y ',' z '};   

There is no null in it. You can either make this a string (because the double-quote is attached to the compiler to automatically connect the tap terminator):

  four dishes [] = "BCD ... z" ;   

Or you can explicitly include yourself at the end of the array:

  four dishes [] = {'B', ' C, D, F, G, H, J, K, L, M, N, P, Q, R , 'S', 't', 'v', 'w', 'x', 'y', 'z', '\ 0'}; Otherwise,  strlen ()  will read well from the end of your array unless there is no byte anywhere in memory with the value of 0.   

Comments

Popular posts from this blog

excel vba - How to delete Solver(SOLVER.XLAM) code -

github - Teamcity & Git - PR merge builds - anyway to get HEAD commit hash? -

ios - Replace text in UITextView run slowly -