The difference between strlen () and sizeof ():
Strlen ()-->c string library function that returns the true length of the string. It starts the scan from a location in memory until the terminator stops and returns the counter value.
The sizeof ()-to operator returns the memory size that is "occupied" after the variable declaration, which may not be the actual size.
int num;
sizeof (NUM); Returns num occupies memory size. sizeof num can also be written (without parentheses when the variable name is followed.) )
sizeof (int); Returns the size of an int data that occupies memory. (when followed by a type name, be sure to add parentheses.) )
The following example is good:
#import<Foundation/Foundation.h>intMainintargcConst Char*argv[]) {@autoreleasepool {CharArr1[] ="Wanger"; NSLog (@"Strlen:%lu___sizeof:%lu", strlen (ARR1),sizeof(arr1)); /*strlen (arr1) ———— > returns the actual length of the string "wanger" stored in arr1. sizeof (ARR1) ———— > Returns the amount of memory occupied by the arr1 declaration of an array. (although there are 6 characters, but the system will automatically add the Terminator ' ", so it is 7)*/ Chararr2[Ten] ="Wanger"; NSLog (@"Strlen:%lu___sizeof:%lu", strlen (ARR2),sizeof(ARR2)); /*sizeof (ARR2) ———— > array ARR2 Declaration when the system allocates 10 bytes to it. */ Char*p ="Wanger"; NSLog (@"Strlen:%lu___sizeof:%lu", strlen (P),sizeof(p)); /*Note: sizeof (P) ———— > It means to return the length of the pointer variable p, we know that the length of the pointer variable is not different depending on the pointer variable type, it is only relevant to the system. */ } return 0;}
The difference between C language--strlen () and sizeof