Two string pointer, magical

In C, string handling is quite complex, can not be directly assigned, unless the character is a pointer to direct assignment. String is usually an array of characters, it said character array have to use strcpy () can be assigned.

Therefore, when the character string processing, often encounter two pointers. Let's summarize what they learned:

1. The characters and character array pointer

The array of characters assigned to a character pointer, can direct assignment, no need to add address-break before & character array.

2. Assignment to the array of characters

Can not directly use the assignment symbol '=', but with strcpy () function.

3. pointer to character pointers

char ** char_pointer = & cell_content_char; This is a two character pointer to the first address pointer;

And * char_pointer character is pointed to by the address stored value, and this character is a character pointer array assignment comes. Therefore cell_content, cell_content_char, * char_pointer value is the same.

4. The pointer assignment to two

(* (* Char_pointer)) = null; This is the value assigned to the null character char_point pointer stored in the first address, i.e., the first character cell_content_char substituted with null and the cell_content.

Output by "i owe" -> "y owe"

The pointer increment

(* Char_pointer) ++; this pointer is moved back one, i.e. the original is the first point of the string, i.e. the position of the character 'i' is now moved back one, the 'location is located. Note that this pointer shift, the shift is cell_content_char, because * (char_pointer) is directed cell_content_char, two known from the address pointer variable.

-> Note and (* (* char_pointer)) ++ distinction! This operation is meaningless.

Then, the assignment of two and a pointer, then the time to replace the character 'y' is in the second original position '.

Thus, the value becomes cell_content_char 'yowe', which is * (char_pointer) value.

And, the value of cell_content, the second bit is changed, the rest of the saved address character has not changed.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define cell_lenght 10

int main(void)
{

	char null = 'y';
	char cell_content[cell_lenght]; //字符数组
	char *cell_content_char = cell_content;//字符数组赋值给字符指针
	char **char_pointer = &cell_content_char;
	//cell_content_char = "i love";
	printf("default value.\n");
	strcpy(cell_content, "i owe");
	printf("cell_content_char = %s, add = %p.\n", cell_content_char, cell_content_char);
	printf("cell_content      = %s, add = %p.\n", cell_content, cell_content);
	printf("*char_pointer     = %s, add = %p.\n", *char_pointer, *char_pointer);
								// char_pointer是指向cell_content_char的指针,保存的是其首地址。
								// *char_pointer      是cell_content_char字符串的值
						
	printf("starting 1st eval test.\n");
	(*(*char_pointer)) = null;  //替换cell_content_char字符串的首字符

	printf("*char_pointer     = %s, add = %p.\n", *char_pointer, *char_pointer);
	printf("cell_content_char = %s, add = %p.\n", cell_content_char, cell_content_char);
	printf("cell_content      = %s, add = %p.\n", cell_content, cell_content);

	printf("starting 2nd eval test.\n");
	(*char_pointer)++;         //将cell_content_char指针移到下一位,(*char_pointer)指的是cell_content_char字符指针和cell_content字符串数组,
								//即指向了cell_content字符串数组"i owe"的第二位 space
	(*(*char_pointer)) = null; //替换cell_content字符串数组第二位空格为null
								//由于指针移到第二位了,所以*char_pointer和cell_content_char的首地址为第二位。
								//cell_content第一位不变,而是第二位插入了null
	printf("*char_pointer     = %s, add = %p.\n", *char_pointer, *char_pointer); 
	printf("cell_content_char = %s, add = %p.\n", cell_content_char, cell_content_char);
	printf("cell_content      = %s, add = %p.\n", cell_content, cell_content);

	return 0;
}

Print Results:

default value.
cell_content_char = i owe, add = 008FF70C.
cell_content      = i owe, add = 008FF70C.
*char_pointer     = i owe, add = 008FF70C.
starting 1st eval test.
*char_pointer     = y owe, add = 008FF70C.
cell_content_char = y owe, add = 008FF70C.
cell_content      = y owe, add = 008FF70C.
starting 2nd eval test.
*char_pointer     = yowe, add = 008FF70D.
cell_content_char = yowe, add = 008FF70D.
cell_content      = yyowe, add = 008FF70C.
ending test.

Released four original articles · won praise 0 · Views 60

Guess you like

Origin blog.csdn.net/IPGsoz/article/details/104885020