当前位置:网站首页>Chapter 7 - pointer learning

Chapter 7 - pointer learning

2022-06-12 05:38:00 Violence produces miracles

Definition and use of pointer variables

#include <iostream>
using namespace std;

int main(){
    
	
	//1、 Definition of pointer 
	
	int a = 10;// Defining integer variables a
	
	// Pointer definition syntax : data type  *  Variable name 
	int * p;
	
	// Pointer variable assignment 
	p = &a;// Pointer to variable a The address of 
	cout << &a << endl;// data a The address of 
	cout << p << endl; // Print pointer variables p
	
	// Use of the pointer 
	// adopt * The memory that the operation pointer variable points to 
	cout << "*p = " << *p << endl;
	
	// Modify the content 
	*p = 100;
	cout << "*p = " << *p << endl;
	cout << "a = " << a << endl; 
	
	return 0;
	 
}

The memory space occupied by the pointer

#include <iostream>
using namespace std;

int main(){
    
	// The memory space occupied by the pointer 
	int a = 10;
	int * p = &a;
	
	// stay 32 Bit operating system , The pointer is occupied 4 Bytes ,64 Under the bit operating system is 8 Bytes  
	cout << "sizeOf(int *)=" << sizeof(int *) << endl;
	cout << "sizeOf(int *)=" << sizeof(float *) << endl;
	cout << "sizeOf(int *)=" << sizeof(double *) << endl;
	cout << "sizeOf(int *)=" << sizeof(char *) << endl;
	return 0;
	
}

Null pointer

#include <iostream>
using namespace std;

int main(){
    
	
	// Null pointer : Pointer to the variable p Point to the memory address numbered 0 Space 
	// purpose : Initialize pointer variables 
	// Be careful : The memory that the null pointer points to is not accessible  
	int * p;
	 
	cout << *p << endl;
	
	return 0;
	 
} 

Wild pointer

#include <iostream>
using namespace std;

int main(){
    
	
	// Wild pointer ; Pointer variable points to illegal memory space 
	// for example 
	int * p = (int *)0x1100;
	
	// Accessing the wild pointer will cause an exception 
	cout << *p << endl;
	
	return 0; 
}

const Modify a pointer

#include <iostream>
using namespace std;

int main(){
    
	
	/*const Three cases of decorating pointers : 1、const Modify a pointer  -- Constant pointer : The direction of the pointer can be changed , But the value pointed to by the pointer cannot be changed  2、const Modifier constant  -- constant pointer : The pointer cannot be changed , The value that the pointer points to can be changed  3、const Modify both pointers and constants  : Neither the pointer nor the value pointed to by the pointer can be changed  */
	int a = 10;
	int b = 10;
	
	const int * p = &a;
	
	//*p=20; error  
	p=&b;
	
	int * const p2 = &a;
	*p2 = 100;
	//p2=&b; error  
	
	const int * const p3 = &a;
	//*p3=100; error  
	//p3=&b; error  
	
	return 0;
} 

Pointers and arrays

#include <iostream>
using namespace std;

int main(){
    
	int arr[] = {
    1,2,3,4,5,6,7,8,9,10};
	
	int * p = arr;// The array name is the first address of the array 
	
	cout << " First element :" << arr[0] << endl;
	cout << " Pointer to the first element :" << *p << endl;
	
	for(int i=0;i<10;i++){
    
		
		// Traversing arrays with pointers 
		cout << *p << endl;
		p++; 
	}
	
	return 0; 
} 

The dynamic array

Using dynamic array to solve the problem of counting the mean and mean square deviation of students' test scores

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

int main(){
    
	int *score, num, i;
	double average = 0, variance = 0;
	
	// Input phase 
	cout << " Please enter the number of people taking the exam :";
	cin >> num;
	score = new int[num];// The dynamic array 
	
	cout << " Please enter the grade :\n";
	for(i=0;i<num;++i){
    
		cin >> score[i];
	}
	 
	// Calculate average 
	for(i=0;i<num;++i){
    
		average += score[i];
	} 
	average = average / num;
	
	// Calculate the mean square error 
	for(i=0;i<num;++i){
    
		variance += (average - score[i]) * (average - score[i]);
	} 
	variance = sqrt(variance)/num;
	
	cout << " The average score is :" << average << "\n The mean square deviation is :" << variance << endl;
	
	return 0;
} 

 Insert picture description here

Pointer function

#include <iostream>
using namespace std;

void swap1(int a,int b);
void swap2(int *p1,int *p2);

// Using pointers as function parameters , You can change the value of the argument  
int main(){
    
	
	int a=10;
	int b=20;
	// Value passed  
	swap1(a,b);
	cout << "a=" << a << endl;
	cout << "b=" << b << endl;
	
	cout << "==========" << endl;
	
	// Address delivery  
	swap2(&a, &b); 
	
	cout << "a=" << a << endl;
	cout << "b=" << b << endl;
	
	return 0;
} 

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

void swap2(int *p1,int *p2){
    
	int temp = *p1;
	*p1=*p2;
	*p2=temp;
}

Using a pointer as a parameter can make a function have multiple execution results

for example : Functions for solving quadratic equations of one variable and their applications

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

int SolveQuadratic(double a, double b, double c, double *px1, double *px2);// Two pointers store two solutions respectively  

int main(){
    
	double a,b,c,x1,x2;
	int result;
	cout << " Please enter a,b,c:";
	cin >> a >> b >> c;
	
	result = SolveQuadratic(a,b,c,&x1,&x2);
	switch(result){
    
		case 0: cout << " The equation has two different roots :x1=" << x1 << " x2=" << x2; break;
		case 1: cout << " The equation has two equal roots :x1=x2=" << x1; break;
		case 2: cout << " There is no solution to the equation "; break;
		case 3: cout << " It's not a quadratic equation of one variable "; 
	}
	return 0; 
}


int SolveQuadratic(double a, double b, double c, double *px1, double *px2){
    
	double disc, sqrtDisc;
	
	if(a==0)
		return 3;// It's not a quadratic equation of one variable 
	disc = b*b-4*a*c;
	if(disc<0)
		return 2;// Rootless 
	if(disc==0){
    
		*px1 = -b/(2*a);
		return 1;// Equal root  
	}
	// Two unequal roots 
	sqrtDisc = sqrt(disc);
	*px1 = (-b+sqrtDisc)/(2*a);
	*px2 = (-b-sqrtDisc)/(2*a);
	return 0; 
}

 Insert picture description here

Divide and conquer finds the maximum and minimum values in an integer array , Store in pointer

#include <iostream>
using namespace std;

void minmax(int a[], int n, int *min_ptr, int *max_ptr);

int main(){
    
	
	int a[] = {
    4,5,1,2,3,9,8,7,0,6};
	int n=sizeof(a)/sizeof(a[0]);
	int min, max;
	minmax(a,n,&min,&max);
	cout << " The maximum is :" << max << "  The minimum is :" << min << endl; 
	return 0;
}

void minmax(int a[], int n, int *min_ptr, int *max_ptr){
    
	int min1, max1, min2, max2;
	
	switch(n){
    
		case 1: *min_ptr = *max_ptr = a[0]; return;
		case 2: if(a[0]<a[1]){
    
			*min_ptr=a[0];
			*max_ptr=a[1];
		}else{
    
			*min_ptr=a[1];
			*max_ptr=a[0];
		}
		return;
		default:minmax(a,n/2,&min1,&max1);// Find the maximum of the first half 、 minimum value 
				minmax(a+n/2,n-n/2,&min2,&max2);// Find the maximum value of the second half 、 minimum value 
				if(min1<min2) *min_ptr=min1; else *min_ptr=min2;
				if(max1>max2) *max_ptr=max1; else *max_ptr=max2;
				return;
	}
}

 Insert picture description here

String as an argument to a function

#include <iostream>
using namespace std;
int word_cnt(const char *); 

int main(){
    
	
	char sentence[80];
	cin.getline(sentence,80);
	const char *s = sentence;
	int res = word_cnt(s);
	cout << " The number of words is :" << res << endl;
	return 0;
}

int word_cnt(const char *s){
    
	int cnt = 0;
	while(*s != '\0'){
    
		while(*s == ' ') ++s; // Skip white space 
		if(*s !='\0'){
    
			++cnt; // Find a word 
			while(*s !=' ' && *s !='\0') ++s;// Skip words  
		} 
	}
	return cnt;
}

 Insert picture description here

原网站

版权声明
本文为[Violence produces miracles]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203010615381444.html