The difference between sizeof and strlen in C + + (c)First, Knowledge Summary:1. sizeof(1), How to usesizeof as operatorsizeof Unary-expressionsizeof (Type-name)(2), Operation stagesizeof is evaluated at compile time.(3), return valuesizeof: Gets the size of the bytes allocated by the object.Using sizeof to return the space occupied by the type and statically allocated objects, structures, or arrays, the return value has no relation to the object, str
Use a knife to kill people, and use no intermediate variable to implement strlen
Sailor_forever sailing_9806@163.com reprint please note
Http://blog.csdn.net/sailor_8318/archive/2008/10/13/3071048.aspx
2009 Tencent campus recruitment pen questions: without using intermediate variables to evaluate the const string length, the strlen function is used to evaluate the string length library. The fun
;Function: the space occupied by the return type of the function. The return type of the function cannot be void.
Ii. strlenStrlen (...) is a function that can be computed only at runtime. The parameter must be a character pointer (char *). When the array name is passed in as a parameter, the array actually degrades to a pointer.Its function is to return the length of a string. The string may be defined by itself or randomly stored in the memory. The function is actually used to traverse from t
Strlen () is a function used in the C language to count650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/80/EA/wKioL1dEczahLZkNAABXPjXpdho337.png "title=" capture. PNG "alt=" Wkiol1deczahlzknaabxpjxpdho337.png "/> In this program, first we write an int type strlen () function, and declare a local pointer variable *char. Strlen () declares an integer cons
Use PHP to get the string length (strlen, mb_strlen. Strlenintstrlen (stringstr) gets the length of the string specified as a parameter. Because the number of bytes is obtained, the halfwidth text is 1 byte and the fullwidth text is 2 bytes. Parameter: nbsp; str object [Translation] use PHP to get the string length (strlen, mb_strlen)
Returns the length of a string.
strlenint
Today, when I realized the strlen function, I met a very interesting warning:warning:deprecated conversion from string constant to ' char* ' [-wwrite-strings]1#include 2 3 intStrlenChar*str) {4 Char*s;5 for(S=STR; *s; s++);6 returnS-str;7 }8 9 intMain ()Ten { One Char*S1 ="ABC"; A CharS2[] ="ABC"; -printf"%d\n", strlen (S1)); -printf"%d\n", strlen
1, sizeof is the operator, strlen is a function;2, sizeof can use the type to do parameters, strlen can only use char* to do the parameters, and must be "" "to end;3, most of the compiler, at the time of compiling the sizeof calculated, this is the length of the type or variable this is the reason that sizeof (x) can be used to define the number of dimensions of the array;4, the results of
Sizeof strlen difference, sizeof Function
This article mainly records the differences between the sizeof operator and strlen () function, and their respective uses. (If you find any mistakes, leave a message to correct them)Differences between sizeof and strlen
The sample code is as follows:
# Include What are the functions of sizeof?
Try replacing
1. sizeof
Its value is calculated at compile time, so it cannot be used to return the size of a dynamically allocated memory stewardess.
When the parameter is the following, the meaning is expressed:
Array-the size of the array space allocated at compile time;
Pointer-The amount of space used to store the pointer (the length of the address where the pointer is stored is a long integer, which should be 4);
Type-the amount of space that the type occupies;
Note: This article is a small series of original, if reproduced, please indicate the source: http://blog.csdn.net/u012116457/article/details/42536039
Today formally began to learn PHP, but the small part accidentally found a use of PHP need to pay special attention to the place.
As this example:
The output of this code is: the length of the name is: 6
Let's explore why this problem occurs: PHP's built-in string length function strlen
more than one byte per assignment of characters to hold the string end flag '/0 '. The actual storage of the above array C in memory is:C P R o g R a m/0'/0 ' is automatically added by the C compiler system. Since the '/0 ' flag is used, it is generally not necessary to specify the length of the array when assigning the initial value to the string, but the system handles it by itself.3. The difference between sizeof and strlenStrlen can only use char* to make arguments, and the char array must
How can strlen () be implemented without using php internal functions? How can strlen () be implemented without using php internal functions?
Reply content:
How can strlen () be implemented without using php internal functions?
function mystrlen($str){ $i = 0; while(1)if(!isset($str[$i++]))return $i-1;}
The efficiency is not high, so it can be improved.
Transfer from http://www.cnblogs.com/mo-beifeng/archive/2011/08/09/2133039.htmlThis is not a fixed number. This article briefly describes the restriction rules.strlen calculate string length, one Chinese when 2 charactersMb_strlen according to its character encoding pattern, the statistic character quotCount counts the number of elements in an array or the number of attributes in an objectHeader (' Content-type:text/html;charset=utf-8 ');$string 1 = "Xiechun industry";//define Chinese character
Just to remember that strlen () return the number of characters of a string. often the strlen () function is used to compute the length in bytes of a string. this is correct until string is single byte encoded. if multi-byte char-set is used this constraint I no more verified. so when you require the number of bytes of A ascii or UTF-8 encoded string, it is better to use following function:
/*** Count the
$ First = 'pear ';$ Second = "Peach ";
// Int strcmp (string str1, string str2)// Case sensitiveEcho "$ first strcmp $ second, result is :";Echo strcmp ($ first, $ second );Echo "/n ";Echo strcmp ("hello", "hello ");Echo "/n ";
// Int strcasecmp (string str1, string str2)// Case NON-SENSITIVEEcho "$ first strcasecmp $ second, result is :";Echo strcasecmp ($ first, $ second );Echo "/n ";Echo strcasecmp ("hello", "hello ");Echo "/n ";
// Int strncasecmp (string str1, string str2, int length)// Cas
Strlen (P):
Can calculate the length of P pointing to a string (starting with the current position of P), and does not contain the terminating character ' \ n ';
P can be declared as char* P or char p[], both of which can be calculated correctly strlen.
sizeof (P):
sizeof is an operator, a non-function, whose value is determined at compile time, so when P is declared as a pointer to a type,
Definition and usage
The
strlenThe length string returned by the () function.
His function is to calculate the length of the word character, Chinese one word is 2Grammar
Strlen (String)
Parameter
Description
String
Required. Specifies the string to check
Let's look at a simple example.
Output to
12
http://www.bkjia.com/PHPjc/445521.html www.bkjia.com true http://www.bkjia.com/PHPjc/445521.html tech
2009 Tencent campus recruitment pen questions: without using intermediate variables to evaluate the const string length, the strlen function is used to evaluate the string length library. The function interface declaration is as follows: int strlen (const char * P );
Train of Thought Analysis:
The last Terminator '/0' can be used in a string, but the parameter here is const, read-only, so we cannot give it
Let's leave it up and go back to the analysis at night.
# Include
# Include
# UNDEF strlen
/*
Return the length of the null-terminated string Str. Scan
The null Terminator quickly by testing four bytes at a time.
*/
Size_t strlen (STR)
Const char * STR;
{
Const char * char_ptr;
Const unsigned long int * longword_ptr;
Unsigned long int longword, magic_bits, himagic, lomagic;
/*
Handle
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.