Chapter 11 strings

String

First, the definition of a string

String is the null character (\ 0) at the end of an array of type char.

Second, the definition string in the program

There are several ways to define strings: String constants, char type arrays, pointer to char.

2.1 String Constants

Content with double quotation marks string literal is referred to, also known as string constants. Double quotes characters and compiler automatically added to the end of the \ 0 character are stored in memory as a string.

String constants are static storage class, indicating that if you use string constants in the function, the string is stored only once, during the life of the whole process of existence.

Double quotes with the contents of the character string is regarded as point storage location pointer.

2.2 initialization string array and

Definition string, you must let the compiler know how much space is required.

Two ways:

Ⅰ. Strings using storage array sufficient space. The char a [50] = "alkdjfaldkfjaldkf";

Ⅱ. Let the compiler determine the size of the array. Such as: char a [] = "aldkjfaldkfalkdfj";

The second convenient method, but this method initialization string can not append new content.

2.3 arrays and pointers

String array declaration: char arr [] = "arry and pointer.";

Pointer declaration string: const char * pt = "arry and pointer.";

The above two statements, pt and arr are the string address. In both cases, the quoted string itself determines the storage space reserved for the string. But there are still two different ways.

** string constants are stored in the static storage area, ** However, the program will allocate memory for an array at runtime, so at runtime to copy the string into an array. At this point there are two copies of the string, a string literal is a static memory, the other is the presence of a string array arr.

And pointers are stored in a static memory location of the string, the string in this way thus only one copy.

Note: arr address constant, and pt is a pointer variable , and therefore this operation can not be performed arr ++, but it may be removed pt ++ element at the next position.

Example 1:

#include <stdio.h>

#define MSG "I'm a const string!"
int main(void)
{
        char arr[] = MSG;
        char * pt = MSG;

        printf("address of MSG is : %p \n", MSG);
    	//指定字符串与 MSG 内容相同
        printf("address of \"I'm a const string!\" is : %p \n", "I'm a const string!");
        printf("address of pt is : %p \n", pt);
        printf("address of arr is : %p \n", arr);

        return 0;
}
/*输出结果:
address of MSG is : 0x400650 
address of "I'm a const string!" is : 0x400650 
address of pt is : 0x400650 
address of arr is : 0x7ffce33adba0 
*/

The results:

Two string constants, like address, description of the same string constants are stored only once (the policy compiler to take relevant); pointer with the address of MSG, as explained pointer is the direct use of the original string, arr with MSG address is not the same, using the described copy arr string.

Constant const is treated as a string data type, and therefore should be a pointer to a const. That is: const char * pt = MSG;

Example 2:

#include <stdio.h>

#define MSG "I'm a const string!"
int main(void)
{
        char arr[] = MSG;
        char * pt = MSG;

        printf("pt[0] is: %s\n", pt);
        printf("pt[1] is: %s\n", ++pt);
        printf("arr[0] is: %s\n", arr);
    	/*输出结果:
    	pt[0] is: I'm a const string!
		pt[1] is: 'm a const string!
		arr[0] is: I'm a const string!
		*/
    
        printf("arr[1] is: %s\n", ++arr);
    	/*编译错误:
    	arr_pt2.c: In function ‘main’:
		arr_pt2.c:12:27: error: lvalue required as increment operand
  			printf("arr[1] is %s\n", ++arr);
  		*/

        return 0;
}

The results:

The results show that pt is variable, arr array name is the address of a constant, but the elements of the array is variable.

2.4 Pointer Array

#include <stdio.h>

int main(void)
{
	int i;
    //声明指针数组
	const char *ptarry[5] = { "aaaaaaaaaaaa", "bbbbbbbbbbbbb", "ccccccccccccccc", "dddddddddddd", "eeeeeeeeeeee" };
    //声明普通数组
	char arry[5][40] = { "aaaaaaaaaaaa", "bbbbbbbbbbbbb", "ccccccccccccccc", "dddddddddddd", "eeeeeeeeeeee" };

	printf("%-36s %-25s\n", "ptarry", "arry");

	for (i = 0; i < 5; i++)
		printf("%-36s %-25s\n", ptarry[i], arry[i]);

	printf("\n size of ptarry is : %zd, size of arry is %zd", sizeof(ptarry), sizeof(arry));

	return 0;
}
/*运行结果
ptarry                               arry                     
aaaaaaaaaaaa                         aaaaaaaaaaaa             
bbbbbbbbbbbbb                        bbbbbbbbbbbbb            
ccccccccccccccc                      ccccccccccccccc          
dddddddddddd                         dddddddddddd             
eeeeeeeeeeee                         eeeeeeeeeeee             

 size of ptarry is : 40, size of arry is 200
*/

operation result:

ptarry size of 40 bytes, and the size is 200 bytes arry (specifically related to the size of the system), because in this system a pointer occupies 8 bytes, i.e. 64 bits, it accounts for 40 5-byte pointer while arry 5 * 40 = 200 bytes.

Reference books

C Primer Plus (sixth edition) Chinese version

Published 42 original articles · won praise 3 · Views 2089

Guess you like

Origin blog.csdn.net/stable_zl/article/details/104094605