C++ Basic Chapeter05 Pointer

1. Defining pointers

int a = 10;
//Grammar of pointer definition: data type * pointer variable name;
int* p;
//Let the pointer record the address of variable a
p = &a;

//Output address in hexadecimal
cout << "a The address is:" << &a << endl;
cout << "Pointer p For:" << p << endl;

// The output results are as follows:

a's address is: 0079FBB0
 Pointer p: 0079FBB0

2. Use pointers
Function: The memory pointed by the pointer can be found by dereferencing
Dereference: * p (add'*'to the front of the pointer) to find the memory data pointed by the pointer

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

The output results are as follows:

a = 1000
*p = 1000

3. Space occupied by pointers
Occupy space:
1) under 32 bit operating system: occupy 4 byte space.
2) under 64 bit operating system: occupy 8 byte space.
3) compile time operation system change mode: x86 is changed to x64 on the solution platform.

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

// The following two outputs are the same
/ / principle: the type of variable p is int*, and sizeof () outputs the space occupied by this pointer.

cout << "sizeof(int*) = " << sizeof(int*) << endl;
cout << "sizeof(int*) = " << sizeof(p) << endl;

The output is: sizeof(int*) = 4

4. Null pointer: pointer points to space numbered 0 in memory
1) Purpose: Null pointer is used to initialize pointer variables
int* p = NULL; // The address of current p is 0

2) Note that null pointers are not accessible
// Memory numbers between 0 and 255 are occupied by the system, so they are not accessible.
// At this point, if you enter: * p = 100; a compilation error will occur

5. Wild Pointer: Pointer Variables Point to Illegal Memory Space

int* p = (int*)0x1100;
cout << *p << endl;

PS: The reason for this code error: After opening up space for the p pointer, it points to another address, but you do not have permission to modify another address.

Note: Neither the null pointer nor the wild pointer is the space we apply for, so don't visit it.

6. const modifies pointers

int a = 10;
int b = 10;

There are three situations:
1) const Modified Pointer-Constant Pointer
// Characteristic: The pointer can be modified, but the value pointed by the pointer can not be changed.

const int* p = &a;

2) const Modified Constant-Pointer Constant
// Features: The pointer can not be modified, but the value of the pointer can be changed.

int* const p = &a;

3) const modifies both pointers and constants
// Characteristic: pointer's direction and pointer's value can't be modified

const int* const p = &a;

7. Pointer and Array

int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };
	cout << "The first element:" << arr[0] << endl;

// Accessing elements in an array with pointers

int* p = arr;//The array name is the first address of the array.
cout << "Access the first element with a pointer:" << *p << endl;
p++;//Four bytes of pointer offset backward
cout << "Access the second element with a pointer:" << *p << endl;
cout << "Traversing arrays with pointers:" << endl;
int* p2 = arr;
for (int i = 0; i < 10; i++)
{
	cout << arr[i] << endl;
	cout << *p2 << endl;
	p2++;
}

8. Pointer and function
1) Value transfer (incoming parameters, arguments unchanged)
2) Address delivery (address of incoming reference parameter/pointer, parameter change)

void swap02(int *p1, int *p2) 
 {
    int temp = *p1;
    *p1 = *p2;
    *p2 = temp;
}
void main()
{
    int a = 10;
    int b = 20;
        
    swap02(&a, &b);
    cout << "a = " << a << endl;
   	cout << "b = " << b << endl;
}

9. Examples of Pointer, Array and Function Combination
Problem Description: Encapsulating a function, using bubble sort to achieve ascending sort of integer arrays
For example, arrays:

//Bubble sort function
void bubbleSort(int* arr, int len)
{
	for (int i = 0; i < len; i++)
	{
		for (int j = 0; j < len - 1; j++)
		{
			if (arr[j] > arr[j + 1])
			{
				int temp = arr[j];
				arr[j] = arr[j + 1];
				arr[j + 1] = temp;
			}
		}
	}
}
//Print array
void printArray(int * arr,int len)
{
   	for (int i = 0; i < len; i++)
   	{
   		cout << arr[i] << " ";
   	}
}
void main()
{
    int arr[10] = { 4,3,6,9,1,2,10,8,7,5 };
    //Array length
    int len = sizeof(arr) / sizeof(arr[0]);
            
    bubbleSort(arr, len);
    printArray(arr, len);
            
    //Blocking programs (usually not used, memory overhead, poor portability)
    system("pause");
}

Added by DaveSamuel on Fri, 04 Oct 2019 17:05:01 +0300