sizeof和strlen的區別


strlen——get the length of a string.
size_t strlen(const char *string);
Each ofthese functions returns the number of characters instring, notincluding the terminating null character.
//函數返回string里的字符數,不包括終止字符'\0'
sizeof
The sizeof keyword gives the amount of storage, in bytes, associated with a variable or atype (including aggregate types). This keyword returns a value of type size_t.
//返回變量或類型(包括集合類型)存儲空間的大小
When appliedto a structure type or variable,sizeof returns the actual size, whichmay include padding bytes inserted for alignment. When applied to a statically dimensioned array,sizeof returns the size of the entire array. The sizeofoperator cannot return the size of dynamically allocated arrays or externalarrays.
//應用結構體類型或變量的時候,sizeof()返回實際大小,包括為對齊而填充的字節。當應用到靜態數組時,sizeof()返回整個數組的大小。sizeof()不會返回動態分配數組或擴展數組的大小。

sizeof與strlen有以下區別:

  • sizeof是一個操作符,而strlen是庫函數。
  • sizeof的參數可以是數據的類型,也可以是變量,而strlen只能以結尾為'\0'的字符串作參數。
  • 編譯器在編譯時就計算出了sizeof的結果,而strlen必須在運行時才能計算出來。
  • sizeof計算數據類型占內存的大小,strlen計算字符串實際長度。
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM