"A Basic C Language Introductory Course"——(10) The pointer of C language is like this

1. Learning objectives

  1. Understand the concept of pointers
  2. Understand how to use pointers
  3. Understand double pointers

table of Contents

First: (a) from the Learning Myth
Two: (b) C language development is not so difficult to simply take you through the process
Title III: (c) easily understand the first C language program
Part IV: ( 4) Basic data types and variables of C language
Chapter 5: (5) Variables, constants and operations of C language
Chapter 6: (6) Easy to understand the logical operations of C language
Chapter 7: (7) C language The loop takes minutes to get started.
Chapter 8: (8) Understanding the basic array is not that simple
. Chapter 9: (9) C language two-dimensional array and loop nesting
Insert picture description here

Second, understand how to use pointers

A pointer refers to a data type that points to a memory address in the C language. In the C language, a variable has a memory address that indicates its storage location. This location is like your house number, which indicates an index to the specific location of your home; for example, a certain village in a certain district of a certain city. What's the number of XX street. The pointer can point to the address where the variable is located, thereby obtaining the value stored at the current address.

2.1 Understanding the use of pointers
First look at how a pointer points to a variable in the C language, the following code:

int *p;
int a=10;
p=&a;

The above code uses int to modify a pointer p to indicate that the pointer points to an integer variable. When declaring, you need to use * plus the variable name to indicate a pointer; then define a variable a with a value of 10; finally use the address symbol & to get the address of a And assign the changed address to the p pointer variable.
The pointer declaration format is:

类型 *指针名;

Let's see a complete example:

#include<stdio.h>
void main(){
    
    
    int *p;
    int a=10;
    p=&a;
    printf("%d\n",*p);
}

I have already understood a part of the above code before. Here, I will focus on the printf("%d\n",*p);output of the value of p in this code. Since my requirement is to output the value stored in the variable a pointed to by the pointer p, the value symbol* is used here. , Which means get the contents of the variable pointed to by the pointer p.

The results are as follows:
Insert picture description here
2.2 Multiple pointers
Multiple pointers are pointers to pointers.
View example:

int *p,**p1;
int a=10;
p=&a;
p1=&p;

In the above code, two pointers are defined, one is p and the other is p1; here you can find that the first p uses a "*", and p1 uses two "*"; use a "*" to indicate the current The pointer points to a variable. The use of two "*" means that the current pointer needs to point to a pointer, and the pointed-to pointer points to a variable; that is to say, the pointer needs to point to a variable, and there is no pointer in the middle, then use a "*" , The pointer points to a pointer, and the pointer points to a variable requires two "*", and so on, one more pointer adds a "*"

Then define a variable a value of 10, use p to point to a, and then p1 to point to the pointer p.
View the full example:

#include<stdio.h>
void main(){
    
    
    int a=10;
    int *p,**p1;
    p=&a;
    p1=&p;
    printf("%d\n",**p1);
}

The results of the operation are as follows:
Insert picture description here
The pointer program for the two pointers is as follows:

#include<stdio.h>
void main(){
    
    
    int a=10;
    int *p,**p1,***p2;
    p=&a;
    p1=&p;
    p2=&p1;
    printf("%d\n",***p2);
}

The results are as follows:
Insert picture description here

Five, summary

Through the above description and explanation, we have learned the following points:

  1. Understand the concept of pointers
  2. Understand how to use pointers and multiple pointers

Guess you like

Origin blog.csdn.net/A757291228/article/details/108899193