[Turn]char * and character array

Source: Internet
Author: User

[Turn]char * and character array

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

The int * I in the code is the focus of our attention. It is a pointer to an int. That is, I points to a memory address, which stores a data from this address. int * I is marked with an int type (length of sizeof (int) bytes) to fetch data from this address, that is, to take the data of sizeof (int) byte at a time to spell the final result. The last example also proves this: if we force a data type of unsigned char to operate on this address, we can only take part of the data out. Conversely, if you use large data types to fetch data that actually stores smaller data types, it is possible to cross-manipulate memory, retrieve some clutter, or cause the system to crash. int b[] This array, marked with a set of numbers, is placed in the memory space with the &b start address, each element occupies a memory unit of sizeof (int) byte, and if there is an operation similar to i=&b;i++;, the value of I increments sizeof (int) each time. Instead of 1, this ensures that I can retrieve exactly the right int every time.

Similarly, char * C is also true. If we define a variable C of char *, then C is simply a pointer to an address in memory. It is indicated that it is a type of char *, that is, to use sizeof (char) as a unit to fetch the number in memory. So, we should call char * C as a pointer to a char type--not to say that C is a string. why pass a char * pointer to a function such as printf (), strlen (), and it can treat it as a string? Yes, don't we define '% ' to represent the end of a "string"? We simply accumulate from the starting address and iterate through each element of the character array until we find a ' + ', even if we are working with a string --the character array element from the start address to ' s ' is a "string", which is the principle of the C language design string.

So, when a function asks for a char parameter, it is not necessarily a string (an array of characters ending with '/'), char * is just a pointer to a character, it simply provides a memory address and an offset for each iteration of the element. What the function does to the incoming parameters, but also depends on the actual implementation of the function. (I think ANSI C should have requirements and provisions for parameters, unfortunately I do not have ANSI C files, can not be referenced.) TheC language Convention is to use char * str to denote an array of characters ending with ' s ', but due to the efficiency of some implementations, this Convention is often not strictly adhered to. C language Design concept does not emphasize that users must use to abide by this Convention, do not abide by this Convention and do not violate the C language basic grammar rules. This may be seen as a feature of the C language and the hacker community that created and used it to advocate freedom, a spiritual culture.

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

[Turn]char * and character array

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.