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 When running on ideone for the above program And when the codeblocks run on windows Please tell me the reason for such strange behavior. . There is no strange behavior here, your strings are not eliminated, hence the When you start your string like this: 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): Or you can explicitly include yourself at the end of the array: 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; }
lenv = 26 and lenc = 21
lenv = 5 and lenc = 26
strlen There is no way for the () function, to identify where they are.
four dishes [] = {'B', 'C', 'D', 'F' , 'J', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's',' t ',' V ',' w ',' x ',' y ',' z '};
four dishes [] = "BCD ... z" ;
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
Post a Comment