[Extended knowledge 2] Use of strlen () and non-function sizeof

Source: Internet
Author: User
[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]

  1. Strlen function
  2. 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:

  1. Sizeof cannot calculate the size of the dynamically allocated array!
  2. 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

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.