The difference and connection between sizeof and strlen

Source: Internet
Author: User

Example:

Char str[20]= "0123456789";
Char str2[20]= "00";
Char str3[] = {1,2,3,4,5,6};
Char str4[] = {0,2,3,4,5,6};
char *STR5 = str2;
char *STR6 = STR4;

1
int A=strlen (str); a=10;
int b=sizeof (str); and b=20;

2
int A2=strlen (STR2); a=2;
int b2=sizeof (STR2); and b=20;

3
int A3=strlen (STR3); a=7;
int b3=sizeof (STR3); and b=6;

4
int A4=strlen (STR4); a=0;
int b4=sizeof (STR4); and b=6;

5
int A5=strlen (STR5); a=2;
int b5=sizeof (STR5); and b=4;

6
int A6=strlen (STR6); a=0;
int b6=sizeof (STR6); and b=4;


As you can see from group 4th, if you strlen () the char array, it will stop when it encounters 0.
As seen from STR2 and STR5, as long as it is a string (including an array of strings), whether or not he is not 0,strlen () will not terminate, it only the end of the statement string to the end flag null.
As you can see from group 6th, if you assign a character array name to a pointer, you still encounter 0
(1) So from the above, for strlen (): As long as the string or pointer string, regardless of whether he contains 0, although with strlen () is. If the pointer address is assigned by a character array, it cannot be used, and it is used even if it is assigned by an array string.

(Returns the length of the string.) The string may be self-defined or random in memory, and the function actually completes the function of iterating from the first address representing the string until the Terminator is encountered null. The returned length size does not include NULL. )
(2) for sizeof (): The array of sizeof, will be the size of the space opened up by arrays, rather than the size of the actual content occupied.

(sizeof cannot be used to return the size of a dynamically allocated memory space.) In fact, using sizeof to return a type and the space occupied by statically allocated objects, structures, or arrays, the return value is not related to what the object, structure, or array stores. )

Also refer to: http://www.cnblogs.com/carekee/articles/1630789.html

The difference and connection between sizeof and strlen

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.