C-language string guide for beginners

Source: Internet
Author: User

Meanings of strings at different levels

1. A string such as "ABC" in the source file
This is called a string literal constant, only inCode. This literal constant is internally linked, and the compiler allocates space for this string within each compilation unit. Because it is a constant, the memory space it is located in is usually not writable.

2. An array of characters ending with '/0' (ascii nul character. Or, more specifically, an array of characters ending with '/0' containing various ASCII printable characters. This is a string definition in the C language. It does not involve compilation or linking, but <string. h> and <stdio. h> many functions in the header file are processed on a time-based basis. When using strlen, strcpy, strdup, and other functions to identify a string, the condition is determined only by the presence of '/0. To a large extent, the existence of the so-called C-style string is embodied by the series of functions <string. h> and <stdio. h>.

The character array, char charray [arraysize], is a lower-level significance. It represents a continuous memory interval that contains char elements. Whether the content is a valid ASCII string and ends with '/0' does not affect its nature as a character array. When this array is allocated as a local or global variable, you can use the sizeof operator to obtain the space occupied by the entire array. This value is not necessarily the same as the strlen of the string contained in it, and is usually not equal.

Char * P =/* initialization */indicates a pointer to a char element. Unreference it to obtain a single char. Because c supports pointer arithmetic operations (+ + P; -- p; P + 2 for char * P), a char * can be used to conveniently access consecutive strings, therefore, char * is usually used to reference a string in a logical sense.

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.