C pointer (ノ゚゚)

Time:2021-1-16

Because I’m learning C pointer recently, so I’ll write a summary here

Hope to help you a little!

There are several types of C pointers

1. Pointer 2. Null pointer 3. Void pointer 4. Const pointer constant

 

Let’s talk about it firstOverview of pointer,

Pointer variablegrammar:

Data type * variable name or data type * variable name = = initial value

The former only defines a pointer to a data type, and the latter defines a pointer to a data type with an initial value

Pointer variables store the addresses of other variables

In C language, there are two pointer operators“*” is an indirect operator and “&” is an address character

Let’s take a simple example

 

The running results of the program are as follows

 

 

Here we find that: the address of a variable = = the value of P variable, that is & A = = P

*P = = the value of a variable

It should be clear about the meaning of “*” and “&”!

In a nutshell*P is the object that the pointer points to

&A is the address where the ﹣ a variable is stored

 

Null pointer:

When defining a pointer, if there is no initialization, then the object pointed to by the pointer is undefined, that is to say, the pointer may point to a piece of memory that has no permission to access. The program will not run normally

So, we initialize the pointer like this

 

 

A null pointer is a null pointer

But because the null pointer is null, it cannot be assigned to other variables as a value

 

Void pointer:

As the name suggests, a pointer to void is called a void pointer. It represents a pointer of undetermined type

Because it is a void type, void pointer is also called universal pointer, which means that it can be converted to any type of pointer for use

 

Const constant pointer:

It is a pointer modified with const, specifically as follows:

  1. Constant pointer 2. Constant pointer variable 3. Pointer constant

const pointer :

Grammar:

    const int *p;

Constant pointer variable:

Grammar:

    int * const p;

Pointer constant:

Grammar:

    const int * const p;

First, constants are immutable

So let’s take a look

const pointer In other words, * int * P is defined as a constant, so * P cannot be changedYou cannot change the object that the pointer points to by * P

 

Constant pointer variableThat is, we define ﹣ P ﹣ as a constant, so ﹣ P cannot be changedYou cannot change the object that the pointer points to

 

as forconstant pointer It’s a combination of the last two,You can’t change the object that the pointer points to or the object that the pointer points to through * P

 

 

                                                                                                                                                                                                                                                               Over!

                                                                                                                                                                                                                                                          Amendment (1)