Summary of C knowledge that needs to be mastered before learning data structure (pointer 1)

Foreword:

Because the book of data structure is very difficult for many people to learn, and data structure is really difficult, because data structure requires a lot of C knowledge to be able to learn, in which pointers and structures are in the data structure described in C The role of is irreplaceable. If you don't understand these things, it is almost difficult to move forward in the book of data structure, which is full of pseudo-code, so I want to summarize some of the knowledge that you need to master before learning data structure.

pointer

Pointer: The required variable unit can be found through the address. It can be said that the address points to the variable unit. We call the address visually as a pointer. (The address of a variable is called the
pointer of the variable ) 1. Pointer variable :
If there is a variable dedicated to storing the address of another variable (ie pointer), it is called a pointer variable.
example:

int a = 19;
int* point_1;
point_1 = &a;
printf("%d,%d,%d,%d\n", a, &a,point_1,*point_1);

Insert picture description here
In this simple program, the results of these four variables can be found, a= point_1, &a=point_1, this is because the address of variable a is assigned to the pointer variable point_1, so if the pointer variable is output in the form of %d, it The value of is actually the address of variable a. When point_1 is pointed to, it is point_1. Because the pointer variable is the address of a, the value of *point_1 is actually the value represented by a. Simply understand, here the pointer variable is the address, and the pointer is the value.

So if the pointer variable does not point to any variable, what will its value be like?
If you run it through the program, you will find that it doesn't work, and the programming software will prompt you that the variable is not initialized, but in fact, if you don't initialize a variable, its value is unknown and random.

2. How to define a pointer variable
type name + * pointer variable name
such as int * point_1
(It should be noted here that the type name is int * instead of int, int * means a pointer to an integer variable, int means defining an integer Type variable, this is not the same, and point_1 is the pointer type name, and the same is true for other types such as character type)
3. For pointer initialization,
you can choose to define a pointer variable first and then assign it, that is, we Said to point it to a variable, we can also assign it directly during definition,
such as: int *point_1=&a;
or
int * point_1;
point_1=&a;
here we call int and other types as base types Don’t forget when you define. The base type is different, which means that the number of bytes represented in their memory is different. For example, the integer data is 4 bytes, and the character type is 1 byte. When we move the pointer, if the base type is In the case of integer, four bytes are moved at a time (as shown in the figure), and the rest is the same;

int a = 19;
int* point_1;
point_1 = &a;
point_1++;
printf("%d,%d\n",  &a,point_1);

Insert picture description here

(It should be noted here that you must not write *point_1=&a; here is wrong, because we know that their types are all different, how can they be assigned?)
4. Reference pointer variable is
for pointer variable point_1 , We quote it in many forms
1. Direct quote:
For example, pirntf("%d",point_1);
can also be quoted in some assignments, such as point_1=&a;
2. Indirect quote
For example:
when writing a band When there is a function with a function body, the pointer points to a certain variable, and for the parameter, whether it is an actual parameter or a formal parameter, what is passed is essentially a pointer, that is, the address of the variable pointed to by the pointer. We call it an address;
5.
Since a pointer array pointer variable can point to a variable, it can also point to an array. In fact, each element in the array has its corresponding address, and the address represented by the variable name can also be said to be in the array The address represented by the first element is actually the address of the array. What we call the pointer points to the array, which means that the pointer points to the address of the first element in the array;

int a[4] = { 1,2,3,4 };
int* p;
p = &a[0];
printf("%d\n", *p);
p = a;
printf("%d\n", *p);

Insert picture description here
Note: The function of p=a; is to assign the address of the first element in the a array to the pointer variable P, instead of assigning all the addresses of all elements in the array to p;
you can also write int * directly when defining the variable p=a;
6. Pointer operations when referencing pointer elements
Pointers are addresses, operations on pointers are essentially operations on addresses;
for example, when p points to a[0], p+1 means a[1] (here Addition, subtraction, multiplication, division, self-addition, and self-decrement are all applicable, and when two pointers are operated on, it only makes sense when they point to the same array at the same time).
Note: The array name is a pointer constant, and its value is fixed during the running of the program, so a++ cannot be realized.
7. Referencing array elements through pointers
(1) subscript method
(2) pointer method
The following two methods are explained through a program:

	int *p,i,a[10];
p = a;
for (i = 0; i < 10; i++)
	scanf_s("%d", p++);

for (i = 0; i < 10; i++, p++)
		printf("%d", *p);
	printf("\n");
for (p=a,i = 0; i < 10; i++, p++)
		printf("%d", *p);
	printf("\n");
for (i = 0; i < 10; i++)
		printf("%d", a[i]);
	printf("\n");
for (p = a, p<(a+10); p++)
	printf("%d,", *p);  

Insert picture description here
It can be seen from the picture that the second line of the output outputs messy things. In fact, it outputs 10 addresses, because when p is auto-incremented, because it is an auto-increment on the address, after the loop, p actually points to It is the last element of the array, so if P is not allowed to re-point, P is incrementing, and it is impossible to predict the address it points to, because it exceeds the effective limit of the array;
8. Pointer operations
mainly need to be explained here. *p++, * (p++), * (++p), ++*p, *++p, ++(*p) Some differences and similarities, let's use a piece of code to conceal these results.

	int *p,i,a[10];
p = a;
for (i = 0; i < 10; i++)
	scanf_s("%d", p++); for (p = a, i = 0; i < 10; i++)
	printf("%d,", *p);       			printf("\n");
	for (p = a, i = 0; i < 10; i++)
		printf("%d,", *p++);			printf("\n");
	for (p = a, i = 0; i < 10; i++)
		printf("%d,", *(p++));			printf("\n");
	for (p = a, i = 0; i < 10; i++)
		printf("%d,", *(++p));			printf("\n");
	for (p = a, i = 0; i <10; i++)
		printf("%d,", ++ * p);			printf("\n");
	for (p = a, i = 0; i < 10; i++)
		printf("%d,", *++p);			printf("\n");
	for (p = a, i = 0; i < 10; i++)
		printf("%d,", ++(*p));

Insert picture description here
The above is almost the result of different operations on the pointer. It should not be difficult for readers to see the reasons caused by each operation method by comparison, so I won't explain it here.
For pointers, knowledge of array names as function parameters is extremely important in the data structure. The organization of these knowledge points will be organized in the next article. Don’t ask why, because it’s too late and I’m going to sleep. Finally, I hope my summary is correct. You helped.

Guess you like

Origin blog.csdn.net/weixin_47160672/article/details/108629971