Based on the basic usage analysis of pointers in C language

Many beginners will send out such feelings: other knowledge in C language can be learned, but the pointer does not understand. If so, I can tell you very responsibly that for this programming language, you are not learning. So learning pointers is very important for beginners. Maybe you think it's a bit abstract, but when you thoroughly understand the meaning of the pointer, you will find that the pointer is particularly convenient to use.

Today I am very superficial about several uses of pointers:

1, the pointer points to the variable:

Here is a code block like this:

Int main()

{int a=10;int b=15;test(a,b);printf("a=%d,b=%d",a,b);}

Void test(int x, int y)

{int tmp;tmp=x;x=y;y=tmp;}

The final output is still a = 10, b = 15. Because when the function is called, only the value is passed between the argument and the formal parameter. But the result of using pointers is different, such as:

Int main()

{int a=10;int b=15;test(&a,&b);printf("a=%d,b=%d",a,b);return 0;}

Void test(int * x, int *y)

{int tmp;tmp=*x;*x=*y;*y=tmp;}

The output is a=15, b=10. The values ​​of variables a and b are swapped. This is because we use pointers to access the storage unit of the variable and indirectly modify the value of the variable.

2, the pointer points to the array:

Define an array and initialize it, int array[5]={2,5,12,7,8}, define a pointer variable and assign the address of the array to it, int *p=array, note that the array name is the address of the array And the address of the array is the address of the first element. So our pointer variable points to the first element of the array, *p=2. If (p+1), then the pointer variable points to the next element of the array, 5, so we can use the pointer to traverse the elements of the array:

Int main()

{int array[5]={2,5,12,7,8};int *p =array;for(int i=0;i<5;i++){printf("array[%d]=%d ",i,*(p+i));}return 0;}

3, the pointer points to the string:

We all know to store strings in arrays, such as char name[20]="jack". The above simply describes the pointer to the array, so we can do this, char *name="jack", the pointer variable points to the beginning of the string. Characters and access to each character of the string in turn.

4, the pointer points to the function:

We need to know how to represent a pointer to a function. To put it bluntly, the syntax is correct. Below I also take a block of code to illustrate:

Int sum(int x, int y)

{return x+y;}

Int main()

{int a=5;int b=6;int (*p)(int,int);p=sum;int result=(*p)(a,b);printf("The result is %d",result );return 0;}

It is not difficult to find that the statement (*p)(a,b) in the above code block can be replaced by p(a,b), because p and sum are the same, but it is easier to understand with the former. And we need to know how to define a pointer to a function, int (*p) (int, int) This is a fixed notation, the previous int is the type of the return value of the function pointed to by the pointer in the future, if there is no function return value, it is void The two ints in the parentheses are of course the formal parameters of the function that the pointer will point to. The pointer to the function is really a bit abstract, so if you want to use it skillfully, do some exercises in this area!

5, the pointer points to the structure:

We first define a structure type first.

Struct student

{

Char *name;

Int ages;

};

Then define the structure variable struct student stu={"Rose",15} according to the type; define a pointer to the structure type, struct student *p; assign the address of the structure variable stu to the pointer variable p, p=&stu; There are 3 ways to access the properties age in the structure:

Stu.ages=15;(*p).ages=15;p->ages=15; but the third way can only be used to point to structures in C language.

In summary, the basic usage of pointers is these, and some small series that are not commonly used are listed here. If you are interested, you can read the relevant information.
Based on the basic usage analysis of pointers in C language

Silicone Rubber Seal ring

Nantong Boxin Electronic Technology Co., Ltd. , https://www.ntbosen.com