An array of strings or string

Array of strings

const char*

Can only be assigned to a string const char * variables can not be assigned char * variable.

	const char* a = "abcde";

Automatically added at the end of the string array '\ 0'. For the above procedures, a [5] is '\ 0', output is empty, after a [5] may be indexed, if the output is a single character, but can not determine what the character.

You may be used strlen () to find the length of the string. End length does not contain '\ 0' is.

	cout << strlen(a) << endl;

An array of strings can be directly output cout

cout << a << endl;

Printf output may also be used

	printf("%s", a);
	printf(a);//以上两种printf都行

const char[]

It must be initialized in the definition. It must be initialized with a string. Only at initialization string can be assigned, and the remaining time can not be assigned.

	const char b[] = "abc";
	b = "def";//错

If initialization is not specified length, the array length defaults to the string length + 1, i.e. including the last '\ 0' length. If the specified length, the array length is the specified length, and the length of the string must be greater than +1 is assigned.

After initialization string, all the remaining bits are initialized to '\ 0'.

Variable type is const char [length of the array]. Available typeid (array name) .name () to get.

Strlen string length requirements may be used. Note that this is the length of the string, not the length of the array. Is the string length to the first '\ 0' in length, not including '\ 0'.

It can be directly output cout or printf

	cout << b << endl;
	printf("%s\n", b);
	printf(b);

An array of characters

char[]

It can not be initialized in the definition. It can be initialized with a string. It can only be assigned at initialization string, and the remaining time can not be so assigned.

	char b[] = "abc";
	b = "def";//错

If initialization is not specified length, the array length defaults to the string length + 1, i.e. including the last '\ 0' length. If the specified length, the array length is the specified length, and the length of the string must be greater than +1 is assigned.

After initialization string, all the remaining bits are initialized to '\ 0'.

May be initialized to empty array, then the specified array length required.

char array[20] = { NULL };

It is a variable type char [array length]. Available typeid (array name) .name () to get.

Strlen string length requirements may be used. Note that this is the length of the string, not the length of the array. Is the string length to the first '\ 0' in length, not including '\ 0'.

It can be directly output cout or printf

	cout << b << endl;
	printf("%s\n", b);
	printf(b);

If within an array of all the '\ 0' are deleted, you can cout or printf, but the output will be hot.

char*

char [] variable (and pointer) may be assigned to char * variable.

	char b[] = "abc";
	char* a = b;

The string can not be directly assigned to char *.

char* a = "abc";//错

char * a need to initialize, or do not know where to point, very dangerous.

	char* a;//此处写法是错误的,不能这样初始化,
	//初始化时需要指向某个地址,否则指针所指地址是不明确的,容易出问题。
	a[0] = 'a';//可以依次对数组元素赋值。
	a[1] = 'b';

If char * a is not initialized, not been assigned, the variable a is not defined, strlen can not be used, can not be directly output cout or printf.
If the char * is initialized or assigned to a char [] variable, strlen may be used, the result is an array to a first '\ 0' length, not including '\ 0'. You can also use cout and printf output directly.

	cout << strlen(a) << endl;
	cout << a << endl;
	printf("%s\n", a);

At this point char of typeid is char .

Pointer address the problem

char [] points to the memory address stored in the string, this time constant is also stored character string storage area.
const char [] points to the memory address stored in the string, this time constant is also stored character string storage area.
const char * address constant points to the storage area, the string is not stored in memory.

	char str1[] = "abc";
	char str2[] = "abc";
	const char str3[] = "abc";
	const char str4[] = "abc";
	const char* str5 = "abc";
	const char* str6 = "abc";
	cout << (str1 == str2) << endl;
	cout << (str3 == str4) << endl;
	cout << (str5 == str6) << endl;

The results are
Here Insert Picture Description

Published 52 original articles · won praise 0 · Views 681

Guess you like

Origin blog.csdn.net/UniversityGrass/article/details/104681744