Storage of strings in memory-advanced C Language

Source: Internet
Author: User

 

A string is a sequence of characters ending with the ASCII character NUL. The ASCII character NUL is represented as \ 0. The string is usually stored in an array or memory allocated from the stack. However, not all character arrays are strings, and character arrays may not contain NUL characters. Character arrays are also used to represent small integer units such as boolean values to save memory space.

C has two types of strings:

Single-byte string

A sequence composed of char Data Types

Wide string

A sequence composed of wchar_t Data Types

The wchar_t data type is used to indicate the width of a character, either a 16-Bit Width or a 32-bit width. Both strings end with NUL. You can find the single-byte string function in string. h, and the wide string function in wchar. h. The wide character is mainly used for non-Latin character sets and is useful for applications that support foreign languages,

The length of a string is the number of characters except NUL. When allocating memory to strings, remember to allocate enough space for all characters plus NUL characters.

NULL is different from NUL. NULL is used to indicate special pointers. It is usually defined as (void *) 0), while NUL is a char and defined as \ 0. The two cannot be mixed!

Character constants are character sequences caused by single quotes. A character constant generally consists of one character. It can also contain multiple characters, such as escape characters. In C, their types are int, as shown below:

printf("%d\n",sizeof(char));printf("%d\n",sizeof('A'));//output//1//4

String Declaration

There are three methods to declare a string: literal, character array, and character pointer.

String Literal is a string sequence that is enclosed by double quotation marks and is often used for initialization. They are located in the string literal pool. <Span style = "color: # ff0000;"> This is different from the single quotes! </Span>

The following is an example of a character array:

char header[32];


The following is a character pointer:

char *header;

String Literal pool

Defining a character volume usually allocates it to the literal volume pool. This memory area stores the character sequences that comprise strings. When the same literal is used for multiple times, the literal pool usually has only one copy. This reduces the memory occupied by applications. Generally, the literal volume is immutable, so there is no problem with only one copy.

The string literal is generally allocated in the read-only area, so it is immutable. It doesn't matter where the string literal is used, or whether it is global, static, or local. From this perspective, the string literal does not have a scope concept.

In most compilers, we regard the string literal as a constant and cannot modify the string. However, the GCC compiler allows the string literal to be modified.

char *header = "Sound";*header = 'L';printf("%s\n",header);//output//Lound


This will change the string, not the expected result. This should be avoided. Declaring a variable as a constant as follows can solve some problems. Any modification will cause a compilation error:

const char *header = "Sound";

String Initialization

The method used to initialize a string depends on whether the variable is declared as a character array or a character pointer. The memory used by the string is either a piece of memory pointed to by the pointer. We can all use strings literally or some column characters to initialize strings, or get characters from other places (standard input.

Initialize a char array

We can use the initialization operator to initialize the char array. In the following example, the header array is initialized as a character contained in the string literal:

char header[] = "Media Player";


The length of the character "Media Player" is 12, indicating that the literal size needs 13 bytes. We need to assign 13 bytes to the array to hold the string. The initialization operation copies these characters to the array and ends with NUL.

We can also use the strcpy function to initialize the array.

Initialize the char pointer

Use dynamic memory allocation to initialize the char pointer.

char *header;char *header = (char*)malloc(strlen("Media Player")+1);

Do not use the sizeof operator. Instead, use the strlen function to determine the length of an existing string. The sizeo operator returns the length of the array and pointer instead of the string.

 

 

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.