Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money.

Pointer to the

Pointers are used to access memory indirectly

  • Memory numbers start from 0 and are usually expressed in hexadecimal numbers

  • You can use pointer variables to store addresses

Definition and use of pointer variables

Define syntax: data type * variable name

“Demo” :

#include <iostream>

using namespace std;

int main(a)
{
    // 1
    int a = 10; // Define the integer variable a

    // Pointer definition syntax: data type * variable name
    int *p;

    // Pointer variable assignment
    p = &a; // Pointer to the address of variable A

    cout << "A's address is:" << &a << endl;
    cout << "Pointer P:" << p << endl;

    // 2
    // Use * to manipulate the memory to which the pointer variable points

    cout << "*p = " << *p << endl;

    return 0;
}
Copy the code

The difference between pointer variables and ordinary variables

  • Normal variables hold data, and pointer variables hold addresses

  • Pointer variables can operate on the memory space they point to through the “*” operator, a process called dereference

summary

  • The address of a variable can be obtained by the ampersand symbol

  • Addresses can be recorded using Pointers

  • To dereference a pointer variable, you can manipulate the memory to which the pointer points

Pointer locks take up memory space

  • All pointer types are 4 bytes on a 32 – digit operating system

  • This is 8 bytes on 64-bit operating systems

Null and wild Pointers

  • Null pointer: The pointer variable points to the space numbered 0 in memory

Purpose: Initialize pointer variables

Note: memory to which a null pointer points is not accessible

“Demo” :

int main(a){

    // the pointer variable p refers to the space with the memory address number 0

    int * p = NULL;

    // Error accessing null pointer

    // Memory numbers 0 to 255 are used by the system, and users are not allowed to access the memory

    cout << *p << endl;

    system("pause");

    return 0;
}
Copy the code
#include <iostream>
using namespace std;

int main(a)
{
    int a = 10;
    int *p = &a; // Pointer to the address of data A

    cout << "P = " << p << endl;
    cout << "*p = " << *p << endl;
    cout << "sizeof(p) : " << sizeof(p) << endl;
    cout << "sizeof(char *) : " << sizeof(char *) << endl;
    cout << "sizeof(float *) : " << sizeof(float *) << endl;
    cout << "sizeof(double *) : " << sizeof(double *) << endl;

    / / null pointer
    //int *q = NULL;
    // cout << "*q=NULL : " << *q << endl; // Segmentation fault (core dumped)

    / / wild pointer
    //int *r = (int *)0x1100;
    //cout << "*r = (int *)0x1100 : " << *r << endl; // Segmentation fault (core dumped)
    return 0;
}
Copy the code
  • Wild pointer: Pointer variable points to an invalid memory space

6. Const decorates the pointer

  • Const decorates pointer: constant pointer
                const int*p = &a ; The pointer pointer can be modified: p = &b; The value to which the pointer points cannot be modifiedCopy the code
  • Const Modifies constants: pointer constants
                int *constp = &a; The pointer pointer cannot be changed; The pointer to the value can be modified: *p =100;

Copy the code
  • Const modifies both Pointers and constants
                const int *constp = &a; The pointer pointer cannot be changed; The value to which the pointer points cannot be changed;Copy the code

“Demo” :

#include <iostream>

using namespace std;

int main(a)
{
    int a = 10;
    int b = 20;
    // Const decorates a pointer. The pointer can be changed, but the value cannot be changed
    const int *p1 = &a;
    p1 = &b;
    cout << "p1 = " << p1 << endl;
    cout << "*p1 = " << *p1 << endl;
    // *p1 = 100; // An error has been reported. The value to which the pointer points cannot be modified
    cout << *p1 = 100: << false << endl;

    // Const modifies constants. The pointer to a pointer cannot be changed
    int *const p2 = &a;
    // p2 = &b; // The pointer pointer cannot be changed
    *p2 = 100;

    // const decorates both Pointers and constants
    const int *const p3 = &a;
    // p3 = &b; / / an error
    // *p3 = 100; / / an error
    return 0;
}

Copy the code

Pointers and arrays

Uses Pointers to access elements in an array

“Demo” :

#include <iostream>

using namespace std;

int main(a)
{
    int arr[] = {1.2.3.4.5.6.7.8.9};
    int *p = arr; // A pointer to an array, which by default points to the first element of the array
    cout << "First element of array arr: arr[0] =" << arr[0] << endl;
    cout << *p = *p << *p << endl;

    int len = sizeof(arr) / sizeof(arr[0]);
    for (int i = 0; i < len; i++)
    {
        // Use the pointer to iterate over the array
        cout << "*p = " << *p << endl;
        p++;
    }
    return 0;
}
Copy the code

Pointers and functions

Use the pointer function argument to modify the value of the argument

“Demo” :

#include <iostream>
using namespace std;

void swap1(int a, int b)
{
    int tmp = a;
    a = b;
    b = tmp;
}

void swap2(int *p, int *q)
{
    int tmp = *p;
    *p = *q;
    *q = tmp;
}

int main(a)
{
    int a = 10;
    int b = 20;
    swap1(a, b); // Value passing does not change the argument
    cout << "Value passing:"
         << "a = " << a << " b = " << b << endl;
    swap2(&a, &b); // Address passing changes the argument

    cout << "Address Transfer:"
         << "a = " << a << " b = " << b << endl;

    return 0;
}
Copy the code

Summary: If you don’t want to change the argument, use value passing; If you want to change the argument, use address passing

9. Pointers, arrays, functions

Case: encapsulate a function that uses bubble sort to sort an integer array in ascending orderCopy the code

“Demo” :

#include <iostream>
using namespace std;

// int * arr[]
void bubbleSort(int *arr, int len)
{
    for (int i = 0; i < len - 1; i++)
    {
        for (int j = i + 1; j < len; j++)
        {
            if (arr[i] > arr[j])
            {
                inttmp = arr[i]; arr[i] = arr[j]; arr[j] = tmp; }}}}void printArray(int arr[], int len)
{
    for (int i = 0; i < len; i++)
    {
        cout << arr[i] << "";
    }
    cout << endl;
}

int main(a)
{
    int arr[] = {1.3.7.2.8.9.6.0.4.5};
    int len = sizeof(arr) / sizeof(int);
    printArray(arr, len);
    bubbleSort(arr, len);
    printArray(arr, len);

    return 0;
}
Copy the code

Summary: When an array name is passed to a function as an argument, it is reduced to a pointer to the first element