C/C + + learning record

Posted by plastik77 on Sun, 28 Nov 2021 10:46:40 +0100

Pointer and array: (access array elements: through subscript / through pointer)

In the array,   The array name is the first address of the array; In combination with the addition and subtraction of the above pointer, access the array 0

Pointer to array:

First declare an array and a pointer variable

int arr[10];
int *p;

Obviously, array arr [] allocates 10 consecutive int memory spaces. The first address (p) of the array is the array name (arr) and the address of the first element of the array (arr[0]); Make the pointer variable p point to the array arr as follows:

//The array name is the first address of the array
p = arr;
//or
//The address of the first element of the array is the first address of the array
p = &arr[0];

Manipulating arrays with pointers:

1.*p = 1

Assignment operation: P points to the first element of the array; (take out the value of P and assign it to 1), that is, arr[0] = 1

2.p±1

Pointer addition and subtraction of integers, movement of memory units* (p+1) = 2, that is, arr[1] = 2

3. Class infers that p+i points to arr[i]

4. The array boundary shall be considered for the operation of p ± 1

Accessing elements of an array with a pointer:

#include<iostream>
#include <string>
using namespace std;

int main() {
	int arr[5] = { 1,2,3,4,5 };
	
    int* p;
	p = arr;
//Or P = & arr [0];
	int i;
	
//Output first element arr[0] = 1
	cout << "arr[0] = " << *p <<"\n";
	cout << "\n";
	
//The for loop outputs all elements
	for (i = 0; i < 5; i++) {
		cout << "arr[" << i << "] = " << *(p+i) << "\n";
	}
}

Output results:

※   Array names are not equivalent to pointer variables; Pointer variables can be added, subtracted and &, which is illegal for array names; At compile time, the array name is a constant

Character pointer and character array:

Character array mode:

#include<iostream>
#include <string>
using namespace std;

int main() {
	char word[] = "point";
	cout << "by total: " << word << "\n" << endl;
	cout << "by letter\n" << endl;
	int i;
	for(i=0;i<sizeof(word);i++)
	cout <<  word[i] << endl;

}

  Output results:

Character pointer mode:

The operation mode is similar; The pointer is the array name of the character array

#include<iostream>
#include <string>
using namespace std;

int main() {
	const char* bird = "parrot";
	cout << bird;
}

Output results:

Pointers and functions:

Pointer as an argument to a function

#include<iostream>
#include <string>
using namespace std;

void swap(int *px,int  *py);

int main() {
	//int* px, * py;
	//※ association between pointer and parameter!
	int x, y;
	int* px;
	int* py;
	px = &x;
	py = &y;
	cout << "please enter two numbers\n";
	//Error record:
	//cin >> *px;
	//cin >> *py;
	cin >> x;
	cin >> y;
	cout << "before:  " << " *px = " << *px << " *py = " << *py <<"\n";
	swap(*px, *py);
	cout << "after:  " << " *px = " << *px << " *py = " << *py << "\n";
}
void swap(int* px, int* py) {
	int temp;
		temp = *px;
		*px = *py;
		*py = temp;
}

Output:

#include<iostream>
#include <string>
using namespace std;

void swap(int x,int  y);

int main() {

	int x, y;
	cout << "please enter two numbers\n";
	cin >> x;
	cin >> y;
	cout << "before:  " << " x = " << x << " y = " << y <<"\n";
	swap(x, y);
	cout << "after:  " << " x = " << x << " y = " << y << "\n";
}
void swap(int x, int y) {
	int temp;
		temp = x;
		x = y;
		y = temp;
}

Output:

  When the parameters passed in are pointers (* px, * py), the values of X and y are exchanged after the swap method is called. If x and y are directly passed in, the name exchange is only valid in swap, and no exchange occurs in main.

Pointer as return value of function:

The statement is as follows:

//Data type * function name (parameter list){
//    Function body
// }

int s;
int *sum(int x,int y){
     s = x + y;
     return &x;
}

Test:

#include<iostream>
#include <string>
using namespace std;

//Declare sum function
int* sum(int x, int y);

int main() {
	int* s = sum(1, 2);
	cout <<  "sum = " << *s;
}

int* sum(int x, int y) {
	int s = x + y;
	return &s;
}

//Error prone:
//int s = sum(1, 2);
    //Error: a value of type int * cannot be used to initialize an entity of type int	

Pointer to function:

A feature of a function: similar to an array - the function name is the entry address of the function.

Functions cannot be nested and defined, and cannot be passed as parameters; you can point to an address through a pointer, and then pass a pointer.

Define format:

data type (*Function pointer name)();

When the "*" operation of the function pointer is performed, the corresponding function can be considered to be executed.

The pointer of the function is different from the pointer of the array. You cannot add or subtract integers.

Topics: C C++