[Extended knowledge 2] Use of strlen () and non-function sizeof [extended Directory] strlen function sizeof (1) function strlen () & #160; & #160; & #160; & #160; prototype: size_tstrlen (constchar * str); & #160; & #160; & #160; & #160; returns a C string (only
[Extended knowledge 2] Use of strlen () and non-function sizeof
[Extended Directory]
- Strlen function
- Sizeof
(1) function strlen ()
Prototype: size_tstrlen (const char * str );
Returns the length of the C string (only this type is supported.
// Use strlen () # include
Int main (void) {chararray [] = "zhijiandeweixiao "; // smile at the fingertip // array is the first address of the array printf ("% s length is % d \ n", array, strlen (array); return 0 ;}
(2) use of non-function sizeof
Sometimes we need the elements of the int type array.
For example, int array [] = {1, 2, 3, 4, 5, 6 ....}; A small number can be counted, but when there is a large number, we have to hand it over to the computer. However, in strlen, only the number of strings is counted. To be able to count the number of int or float arrays, you have to start your mind.
The effective method is as follows:
// Use of Non-function sizeof # include
Int main (void) {intnum; intarray [] = {1, 2, 3, 4, 5, 6, 7}; // sizeof (array) = 28 bytes // sizeof (array [0]) = 4 bytes // so sizeof (array)/sizeof (array [0]) = 7, that is, the number of array elements num = sizeof (array)/sizeof (array [0]); printf ("sizeof (array) = % d \ n", sizeof (array )); printf ("sizeof (array [0]) = % d \ n", sizeof (array [0]); printf ("sizeof (array) /sizeof (array [0]) = % d \ n ", num); return 0 ;}
Note:
- Sizeof cannot calculate the size of the dynamically allocated array!
- When an array is used as a function parameter, you cannot pass the array parameter itself to tell the function the size of the array when running the program, because the array parameter of the function is equivalent to a pointer pointing to the first element of the array.
// Use non-function sizeof 2 # include
Int main (void) {intnum; intarray [] = {1, 2, 3, 4, 5, 6, 7 }; // array is the first address of the array // array is the first address of the first element of the array // sizeof (array) = 28 bytes // sizeof (array [0]) = 4 bytes // so sizeof (array)/sizeof (array [0]) = 7, that is, the number of array elements num = sizeof (array) /sizeof (array [0]); printf ("sizeof (array) = % d \ n", sizeof (array); printf ("sizeof (array [0]) = % d \ n ", sizeof (array [0]); printf (" sizeof (array)/sizeof (array [0]) = % d \ n ", num); return 0 ;}
Running result:
Sizeof (array) = 28
Sizeof (array [0]) = 4
Sizeof (array)/sizeof (array [0]) = 7
// Use non-function sizeof 2 # include
Void print (int array []) {int num; num = sizeof (array)/sizeof (array [0]); printf ("num = % d \ n ", num); return;} int main (void) {intarray [] = {1, 2, 3, 4, 5, 6, 7}; print (array ); return 0 ;}
Running result:
Num = 1
The result shows that when an array is used as a function parameter, the array parameter is equivalent to a pointer to the first element of the array, so the size is 1, that is, the first element.
[Smile at your fingertips] Errors are inevitable. I hope you can get your corrections ^-^
Reprinted when retaining the original link http://codingit.howbbs.com and http://blog.csdn.net/mirrorsbeyourself