Char * and character array, char character array

Source: Internet
Author: User

[Convert] char * and character array, char character array
[Convert] char * and character array

Address: http://www.cnblogs.com/jeakon/archive/2012/05/27/2816809.html

 

Int * I in the Code is our focus. It is a pointer to an int. That is to say, I points to a memory address, from which a data is stored. Int in int * I indicates that int type (Length: sizeof (int) bytes) should be used to retrieve data from this address. That is to say, sizeof (int) should be used once) bytes to form the final result. The last example also proves that if we force the unsigned char data type to operate on this address, we can only retrieve part of the data. Conversely, if a large data type is used to retrieve data of a smaller data type, the memory may be out-of-bounds operations, and some messy content may be retrieved or the system may crash. Int B [] indicates a group of numbers in the memory space starting with & B. Each element occupies memory units of sizeof (int) bytes; if there is an operation similar to I = & B; I ++;, the value of I increments by sizeof (int) instead of 1, this ensures that I can retrieve a correct int each time.

Similarly, char * c does the same. If we define a char * variable c, then c is just a pointer to an address in the memory. It indicates that it is of the char * type, that is, it is necessary to take the number in memory as the unit of sizeof (char. Therefore, we should call char * c as a pointer to the char type -- instead of c as a string. Why is it possible to pass a char * pointer to functions such as printf () and strlen () as a string? That's right. Didn't we define '\ 0' to indicate the end of a "string? We only need to accumulate from the starting address and traverse every element of the character array until a '\ 0' is found, even if a string is processed --The character array elements from the starting address to '\ 0' constitute a "string", which is the principle of the C language string design.

Therefore, when a function requires passing in a char * parameter, it is not necessarily a string (an array of characters ending with '\ 0 ), char * is just a character pointer. It only provides a memory address and the offset of each element traversal. What are the requirements of a function for input parameters? depends on the specific implementation of the function. (I think ansi c should have some requirements and provisions on parameters. Unfortunately, I do not have the ANSI C file and cannot refer to it. )The C language generally uses char * str to represent the character array ending with '\ 0'. However, due to some implementation efficiency considerations, this Convention is often not strictly followed. The design concept of C language does not emphasize that users must abide by this agreement, and does not violate the basic syntax rules of C language. This may be seen as a special and spiritual culture that the C language and the hacker community that creates and uses it advocate freedom.

 

Trackback: http://blog.vckbase.com/smileonce/archive/2005/06/26/8330.html

 

Related Article

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.