C language-1. First understanding of pointers and pointer variables

Memory = "hotel"
memory allocated space for bytes in memory = address = "room number"
data stored in the address = variable/constant = "traveler"
It can be said that the address points to the variable unit; for example, room 1110 lives in Xiaoming, then 1110 is the address; Xiaoming is the variable stored at this address; 1110 points to Xiaoming; therefore, the address is visualized as a pointer;
here we must understand the two concepts of the address of the storage unit and the content of the storage unit, the compiler is compiling After that, the access to the variable is carried out through the address of the variable. For example, scanf ("%d,&i") sends the value input from the keyboard to the storage unit starting with 2000,
such as the statement a=i; , then by accessing the address where i is located, the value in the address is sent to the address where a is located, which is called direct access ;
there is also an access method called indirect access , that is, the address of variable i is stored in another variable, Then use the variable to find the address of the variable i, so as to access i;
for example, int *p;
p = &i;
here is to store the address of i in p; p = 2000 at this time, that is, the variable i The starting address of the occupied memory unit;
this is the meaning of the pointer;
then there are two ways to assign the value 3 to the variable i;
1. Direct access: i=3;
2. Indirect access: int *p;
*p = 3;
p=&i;
Distinguish pointers and pointer variables:
1. The address of a variable is the pointer of the variable; for example, the address 2000 is the pointer of the variable i;
2. The address (pointer) specially used to store another variable, then Call him a pointer variable;
To put it vulgarly: the pointer is determined by the system. After the system compiles the program, what is the address assigned to the variable, the address of the variable (how much is the pointer)
, and the name of the pointer variable is defined by the user in the program. It can be p, can be p1; it is specially used to store the address of the variable; it is a variable, and it also needs to define the type;
then, we can analyze an example to learn the definition and use of pointer variables;

#include<stdio.h>
int  main()
{
    
    
	int a = 100, b = 10;
	int *p1, *p2;//定义指针变量;不指向任何变量;
	p1 = &a;//p1指向a;p2指向b;
	p2 = &b;
	printf("a = %d,b=%d\n", a, b);
	printf("*p1 =%d,*p2 =%d",a, b);
	return 0;
}

The result after compilation is as follows: insert image description herethe 4th line of the program defines the pointer variable; p1 and p2 are the names of the pointer variables; the
56th line of the program, the use of the pointer variable

Guess you like

Origin blog.csdn.net/jinanhezhuang/article/details/119000633