A detailed talk about the difference between sizeof and strlen and its connection

Source: Internet
Author: User

sizeof is a C + + in the keyword , which is an operator whose purpose is to obtain the length of an object ( data type or data Object ) ( That is, the size of the memory that is occupied, in bytes .

When the arguments are as follows, the value returned by sizeof represents the following meanings:
       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 the type occupies;
        Object-The actual amount of space the object occupies;
         Function-- void

strlen is a function that can be evaluated at run time. The argument must be a character-type pointer ( char*). When an array name is passed in as a parameter, the array is actually degenerate into a pointer.
Its function is: 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 .

Here are the differences between sizeof and strlen :
1.sizeofis an operator,strlenis a function.  
2.sizeofarguments can be made with a type or function,strlencan only be usedchar*parameters, and must be in the "" "end of .  
3.Array to dosizeofthe parameters are not degraded and passed tostrlenThe pointer is degenerate ( the array is passed as a parameter to the function simultaneous is a pointer instead of an array, passing the first address of the array).

Char s []= "12233w4q";

Char BUF1 [n];

    char buf2 Span style= "COLOR: #4C4C4C" >[ 50 ]={ Span style= "COLOR: #C68C8C" >1 2 Span style= "COLOR: #4C4C4C", 4 Span style= "COLOR: #C68C8C" >5 };

printf ("%d,%d,%d\n",sizeof(s),sizeof( buf1),sizeof(buf2));

// Output : 9,,,

printf ("%d,%d,%d\n",strlen(s),strlen( buf1),strlen(buf2));

// Output : 8, the , 4  

//result of the second output theeach run may actually be different, depending onBUF1What's in there?
4.strlenresults are calculated at run time, and are used to calculate the length of the string, not the size of the memory.  
5.sizeofIf the type must be bracketed, the variable name can be non-parenthesized. This is becausesizeofIt's an operator, not a function ..  
6.when applied to a struct type or variable, sizeofreturns the actual size, when applied to a static space array, sizeofreturns the dimensions of all arrays.  sizeofThe operator cannot return the dimensions of an array or an outer array that has been dynamically dispatched .

A detailed talk about the difference between sizeof and strlen and its connection

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.