C language_ Pointer one

Posted by daeken on Wed, 02 Feb 2022 15:47:16 +0100

Pointer one

I The concept of pointer

1. In order to facilitate access to the contents of memory, give each memory unit a number, which we call the address, that is, the pointer

The address is the pointer

2. The pointer is also a data type, so the pointer has its own memory and stores the address (number)

Four elements

int a=10

int* p=&a;===> Int * is the type of the pointer itself, P is the pointer name, and int is the type pointed to by the pointer

1. Type of pointer itself

Apart from the pointer name, the rest is the type of the pointer itself

2. Type of pointer

Apart from the pointer name and a *, the rest is the type pointed to by the pointer

3. Memory of pointer itself

Used to store the address pointing to the content (in the pointer, only the number) (four bytes)

4. Memory pointed by pointer

It can be of various types

II. Definition of pointer

1. Operator

When defining, it means to define a pointer, and other times it means to resolve the reference (get the content)

&: first address character

definition

Define a pointer, which is also a data type

Pointer type = = "pointer name"

1. Remove the pointer name, and the rest is the pointer type

2. Remove the pointer name and a *, and the rest is the type pointed to by the pointer

int* p;//Field pointer
//Defines a pointer of type int * called p
//The pointer p itself is of type int*
//p this pointer points to int

Three pointer memory

The pointer is also a data type, so the pointer has its own memory and stores the address (number)

The number is a number = = "hexadecimal number

1. All pointers, regardless of type, only occupy 4 bytes of memory in memory

It depends on the specific compiler

    int* a;
    short* b;
    char* c;
    float* d;
    double* e;
    printf("%d\n",sizeof(a));
    printf("%d\n",sizeof(b));
    printf("%d\n",sizeof(c));
    printf("%d\n",sizeof(d));
    printf("%d\n",sizeof(e));
//All four bytes,

Initialization and assignment of four pointers

1. Assign a value with the address of the corresponding type variable

int num=10;
int* p=#//initialization
//It must be the corresponding type and can be forced to convert
​
short sh=20;
short* p1;
p1=&sh; //assignment

2. Give values directly with pointers of the same type

int num=10;
int* p=#
int* p1;
p1=p;
int* p1=p;


3. Directly use the address to give the value

int* p=(int*)0x36;
//(int *) = = > cast type

4. Give values with array names

//The array name is the first address of the array!!!
int arr[5]={1,2,3,5,6};
int* p=arr;
//*p takes the first content in the array, 1

5. Give value with string

char arr[10]="abcd";
char* p=arr;
char* p="abcd";
printf("%x",p);//Printed is a hexadecimal number, which is the address of p
printf("%s",p);//It's abcd
cout<<p<<endl<<;//It prints abcd
​
//The pointer of char * type can directly output the whole string until it stops at '\ 0'

6. Empty

1. Sometimes, the pointer is defined but not pointed to,

2. Or, the pointer is used up and there is no point

I don't know where to point at this time

The pointer at this time is very dangerous = = "(wild pointer)

So, for the pointers in these cases, we give them a fixed point

Point to 0 address

int* p1=NULL;
int* p2=nullptr;
int* p3=(int*)0x0;
char* p4=(char*)0x0;

7. Multi level pointer

int a=10;

int* p=&a;

*p refers to taking out the things stored in the memory pointed to by p

And p points to a, where 10 is stored

So the value of * p printed is 10

int a = 10;
    cout << "a=" << a << endl;
//a=10
    cout << "&a=" << &a << endl << endl;
//address
    
​
    int* a1 = &a;
    cout << "a1=" << a1 << endl;
//a1 is a pointer that prints the address
    cout << "&a1=" << &a1 << endl;
//Take the first address of a1
    cout << "*a1=" << *a1 << endl << endl;
//*a1 is an int integer, * a1=10
​
    int** a2 = &a1;
    cout << "a2=" <<a2<< endl;
    cout << "&a2=" << &a2 << endl;
    cout << "*a2=" << *a2 << endl << endl;
​
    int*** a3 = &a2;
    cout << "a3=" << a3 << endl;
    cout << "&a3=" << &a3 << endl;
    cout << "*a3=" << *a3 << endl <<;
    cout<<"***a3="<<***a3<<endl;
​
/*Printed results
a=10
&a=00F7FA44
​
a1=00F7FA44
&a1=00F7FA38
*a1=10
​
a2=00F7FA38
&a2=00F7FA2C
*a2=00F7FA44
​
a3=00F7FA2C
&a3=00F7FA20
*a2=00F7FA38
***a3=10
*/

An example

    short sh = 10;
​
    short* psh1 = &sh;
    short* psh2 = psh1;
​
    short** ppsh1 = &psh2;
    short** ppsh2 = ppsh1;
​
    short*** pppsh = &ppsh2;
​
    ***pppsh = 9;
    cout << "sh=" << sh << endl;
//The last output sh=9, * * * pppsh refers to the content in SH directly and can be modified directly

V Addition and subtraction of pointer

Core: does the value (pointing) of the pointer itself change

Offset of pointer

Do not change the direction of the pointer

1. The pointer can add or subtract an integer

2. After adding or subtracting an integer, the pointer is actually offset

The offset range is an integer unit of addition or subtraction

Unit: the number of bytes in memory of the type pointed to by the pointer

Offset: the pointer point remains unchanged, but the content is retrieved according to the offset

    int num = 10;
​
    int* p = &num;
    cout << p << endl;              //004FF728
    cout << "*p="<< *p << endl;     //*p = 10
    cout << p + 1 << endl << endl;  //004FF72C added 4
​
    short sh = 20;
    short* psh = &sh;
    cout << psh << endl;            //004FF710
    cout << psh + 1 << endl;        //004FF712 added 2

Vi Self increasing and self decreasing of pointer

Self increasing and self decreasing will change the direction

++: indicates that the pointer moves back one unit

--: indicates that the pointer moves forward one unit

Unit: the number of bytes in memory of the type pointed to by the pointer

VII Pointer and one-dimensional array

Traversing a one-dimensional array through a pointer

    int arr[5] = { 1,2,3,6,5 };
    int* p = arr;
    cout << *p << endl;//Print 1
    for (int i = 0; i < 5; i++)
    {
        cout << arr[i] << endl;
    }
    int* parr = arr;
    for (int i = 0; i < 5; i++)
    {
        cout << parr[i] << endl;
    }
    printf("\n");
    //The output effect is the same
    //parr and arr are the same
    //But arr is a constant
    cout << *(parr+0) << endl;
    cout << *(parr + 1) << endl;
​
    /*
    *(parr+0) <==> parr[0]
    *(parr+1) <==> parr[1]
    *(parr+2) <==> parr[2]
    Conclusion:
    *(p+n)Equivalent to p[n]
    */


Traversing a two-dimensional array through a pointer

int arr[3][4] = {
    {1,5,3},
    {6,3,2},
    {5,6,9}
    };
    /*
    int* p = &(arr[0][0]);
    *(p+n) <==> p[n]
    arr[0][0]  ==>  *(arr[0]+0) ==> *(*(arr+0)+0) ==> *(*(arr))  
    &(*(*(arr)))==>(*(arr))==>*(arr)==>arr[0]
    */
    int* p = arr[0];
    for (int i = 0; i < 12; i++)
    {
        cout << p[i] << endl;
    }
//You can print this two-dimensional array

Storage mode

1. Small end mode (vs)

Storage: low order of low byte storage data

Read: read from high to low (logic habit)

One hexadecimal corresponds to four binary

Two hexadecimal digits correspond to one byte

2. Big end mode

Storage: high bit of low byte storage data

Reading: reading from low to high (reading habit)

Topics: C