C Language Basics Tutorial (iv) Pointers, structures, unions, and enumerations (7)

Source: Internet
Author: User
Tags constant
3. Character pointer
As we already know, string constants are sequences of characters enclosed in double quotes, such as:
"A String"
is a string constant that consists of a sequence of 8 characters because there is a space character behind the character A. In the program, such as the occurrence of string constants C compiler to the string constants in a storage area, the area is static, throughout the process of running the entire program is always occupied, usually the length of the string constant refers to the number of characters of the string, but in the row storage area, The C compiler also automatically adds a null character ' "to the end of the string sequence to flag the end of the string, so that a string constant occupies a byte more than the number of characters in the storage area.
The method for manipulating a string constant in Turbo C is:
(1). Store string constants in an array of characters, for example:
Char s[]= "a string";
Array s consists of 9 elements, of which the contents of s[8] are '. In fact, in the process of defining a character array, the compiler directly carbons the string into an array, which is the initialization of the logarithmic group S.
(2). Use the character pointer to point to the string, and then access the string storage area through the character pointer. When a string constant appears in an expression, it is converted to a character pointer, based on the type conversion rule of the array. So, if we define a character pointer CP:
Char *CP;
It is then available:
Cp= "a string";
Make CP Point No. 0 character A in a string constant, as shown in Figure 7.
Cp
┏━━━┓┏━┳━┳━┳━┳━┳━┳━┳━┳━┓
┃─╂─→┃a┃┃s┃t┃r┃i┃n┃g┃\0┃
┗━━━┛┗━┻━┻━┻━┻━┻━┻━┻━┻━┛
Figure 7.
Later we can access this storage area through the CP, such as *CP or Cp[0] is the character a, while cp[i] or * (cp+i) is equivalent to the character of the string, but attempts to modify the string constants through the pointer are meaningless.

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.