c ++ the pointer

First, the basic concepts of the pointer

Role pointer: Pointer can indirectly access memory.

  • Memory is numbered from 0 to start recording, usually expressed in hexadecimal numbers.
  • You can use the pointer variable holds the address.

Second, the definition and use of a pointer variable

Pointer variable defines the syntax: data type variable name *

    int a = 10 ;
     // declare pointer variable 
    int * P;
     // pointer to the address of a 
    P = & a ;
     // use * dereference a pointer to find the data in memory 
    cout << * p << endl;

Third, the memory space occupied by the pointer

In the 32-bit operating system, regardless of what the data type is a pointer data type is 4 bytes, 8 bytes in the 64-bit operating system.

Fourth, the null pointer and field guide

1. null pointer : a pointer variable to point to memory spaces numbered 0.

Uses: initialize the pointer variable.

Note: null pointer memory is not accessible.

int * the p-= NULL;
 // * the p-; the memory number between 0-255 memory is occupied by the system and can not access

2. wild pointer : pointer variable points to an illegal memory space

// in the program to avoid wild pointer 
int * P = ( int *) 0x1100 ;

Five, const pointer modification

const pointer modified in three ways:

  • const modifier pointer - pointer constant
  • const modified constant - pointer constant
  • const pointer modification, and modified constants
    int A = 10 ;
     int B = 10 ;
     // constant pointer, can not change the value of the pointer, a pointer can be changed
     @ i.e., * p = 20 is illegal, p = & b is legal 
    const  int * P = & a ;
     // pointer constant, not changing pointers, point value can be changed
     // i.e. * p2 = 20 is legal, p2 = & b is not legal 
    int * const P2 = & a ;
     // value pointer and do not change the value points
     // i.e. * p3 = 20 is not legal, p3 = & b is not legal 
    const  int * const P3 = & a;

Six, with the pointer array using

1. The values ​​in the array can be accessed by a pointer

int main () {
     int ARR [] = { . 1 , 2 , . 3 , . 4 , . 5 , . 6 };
     // address of the array name is the first element of the array 
    int * P = ARR;
     // the pointer to access the array the first element
     // dereference extracted four bytes of data 
    COUT * p << << endl;
     // because p is a pointer to an integer, then p ++ executed, the pointer to the next element is 
    p ++ ;
     // using the pointer to access a second array element 
    COUT * P << << endl;
}

2. pointers to traverse an array

    // the pointer through the array: 
    int * P2 = ARR;
     for ( int I = 0 ; I <( the sizeof (ARR) / the sizeof (ARR [ 0 ])); I ++ ) {
        cout << *p2 << endl;
        p2++;
    }

Seven pointers and function

The main difference is passed by reference or value transfer

1. The value is passed: the change parameter value does not affect the passed argument

#include <iostream>
using namespace std;

void swap(int num1, int num2) {
    COUT << " value before switching the num1: " << num1 << endl;
    COUT << " value before switching num2: " << num2 << endl;
     int tmp = num1;
    num1 = num2;
    num2 = tmp;
    COUT << " value after the switching num1: " << num1 << endl;
    COUT << " value after the switching num2: " << num2 << endl;
}

int main ()
{
    int a = 1;
    int b = 2;
    COUT << " argument is passed in a previous value of: " << a << endl;
    COUT << " before the value is passed in the argument of b: " << b << endl;
    swap(a, b);
    COUT << " A value after incoming arguments: " << A << endl;
    COUT << " After the incoming argument value of b: " << b << endl;
    system("pause");
    return 0;
}

Output:

2. Reference transfer: change the value of the parameter will affect the value of the argument;

#include <iostream>
using namespace std;

void swap(int* num1, int* num2) {
    cout << " before the exchange value of num1: " << * num1 << endl;
    COUT << " num2 value before the exchange: " << num2 * << endl;
     int tmp = * num1;
     * * = num1 num2;
     num2 = * tmp;
    cout << " value after the exchange of num1: " << * num1 << endl;
    cout << " value after the exchange num2: " << * num2 << endl;
}


int main ()
{
    int a = 1;
    int b = 2;
    int* p1 = &a;
    int* p2 = &b;
    COUT << " argument is passed in a previous value of: " << a << endl;
    COUT << " before the value is passed in the argument of b: " << b << endl;
    swap(p1, p2);
    COUT << " A value after incoming arguments: " << A << endl;
    COUT << " After the incoming argument value of b: " << b << endl;
    system("pause");
    return 0;
}

Output:

 

 Eight, and an array of function pointers

A packaging function, refers to the use of the bubble sort for arrays.

#include <iostream>
using namespace std;

void bubbleSort ( int * P, int length) {
     // If the length of the array is calculated here, the output is 1, is the first to come because the incoming address of the array, which is stored only one element
     // int len = the sizeof (ARR ) / the sizeof (ARR [0]);
     // COUT len << << endl;
     // ARR is a pointer to the first element of the array, the value of the standard used at the access element array, the equivalent pointer to the memory element
     // my understanding that c ++ will position themselves to that, and to obtain values 
    for ( int I = length - . 1 ; I> = 0 ; i-- ) {
         for ( int J = I - . 1 ; J> = 0 ; J, ) {
             IF (P [I] < P [J]) {
                int tmp = p[i];
                p[i] = p[j];
                p[j] = tmp;
            }
        }
    }
}
void printArr(int* arr, int length) {
    for (int i = 0; i < length; i++) {
        cout << arr[i] << endl;
    }
}

int main () {
     int ARR [] = { . 1 , . 4 , 2 , . 7 , . 6 , . 3 , . 5 };
     // Note that the length of the array to be calculated outside the function
     // If the function which is calculated, since the array pass in is the address of the first element, the length always. 1 
    int length = the sizeof (ARR) / the sizeof (ARR [ 0 ]);
    bubbleSort(arr, length);
    printArr(arr, length);

    system("pause");
    return 0;
}

Output:

 

Incoming arrays are passed by reference, and therefore which changes the value of the original will affect the value of the array arr, arr so in the end it can be printed out directly. 

Guess you like

Origin www.cnblogs.com/xiximayou/p/12080180.html