Otaru C++ Chapter 8 (1) Pointer Variables

(1) Pointer variable

1. Concept and definition of pointers

2. Assign a value to the pointer variable p

3. + and - operations on pointer variables

4. Typeless pointers

5. Multiple pointers


Otaru C++ Multi-Chapter 8 (2) Pointers and Arrays icon-default.png?t=N176https://blog.csdn.net/weixin_44775255/article/details/129396791

Otaru C++ Multiple Chapters ⑧ (3) Pointers and Strings, (4) Functions and Pointers icon-default.png?t=N176https://blog.csdn.net/weixin_44775255/article/details/129397866

 C++ (1) Pointer variables

Xiao Ming wanted to return the book "CCF Computer Programming for Middle School Students" borrowed from Li Hua's home to Li Hua, but Li Hua was not at home, so he put the book on the far right of the third floor of the bookshelf and wrote a message there On the table, it says: I'll return your book and put it on the far right of the third floor of the bookshelf. When Li Hua came back, he knew where the book was when he saw this message.

What is the purpose of this message?

It is equivalent to a pointer. The content on it is not the book itself, but the location of the book. Li Hua found the book returned by Xiao Ming through the message note (pointer).

The pointer targets the location (address) of the memory space, and the address operator <--> &

int a=3; How to find the address location where a value is 3?

#include<iostream>
using namespace std;
int main(){
	int a=3;
	cout<<&a<<endl;
	a=4;
	cout<<&a<<endl;
	int b=3;
	cout<<&b<<endl; 
	return 0;
}

  • It can be found that the address of variable a has nothing to do with the value assigned! ! !
  • The memory space addresses of different variables are different! ! !

1. Concept and definition of pointers

Pointer definition: variable type *p; --》int *p  = new(int); //Apply a space for p, the content of *p is uncertain.

int *p = NULL;

A pointer variable p is defined, p points to a memory space, and is assigned a value of NULL, which is 0, indicating a special empty address.

2. Assign a value to the pointer variable p

p=&a;   //Give p the address where variable a stores the value. Note that p is the address, and the value of pointer *p is 3.

 

 Exercise 1. Input two different floating point numbers, add the maximum value + 10 through the pointer, and output it.

#include<iostream>
using namespace std;
int main(){
float a,b,*p;
	cin>>a>>b;
	if(a>b){
		p = &a;
	}
	else{
		p = &b;
	}
	*p += 10;
	cout<<*p<<endl; 
	return 0;
}

 


 3. + and - operations on pointer variables

Exercise 2: Input n integers and use pointer variables to access the output

#include<iostream>
using namespace std;
int main(){
 	int n,a[100],*p;
	cin>>n;
	for(int i=0;i<n;i++){ //输入 
		cin>>a[i];
	} 
	p = &a[0]; //初始赋值 
	for(int i=0;i<n;i++){
		cout<<*p<<" "; 
		p++; // p++指的是对地址+sizeof(int) 
	} 
	return 0;
}

If pointer p = &a[0];

So what does *p+3 mean? What about *(p+3)?

 *p represents value *p+3 represents value +3; *(p+3) p represents address, p+3 represents address+3

int a[1000];

p = &a[5];

p++ is equivalent to a[6]

Exercise: *p+3 is equivalent to  a[5]+3    ; *p-2 is equivalent to  a[5]-2    ; *(p-2) is equivalent to  a[3]     .


4. Typeless pointers

Sometimes we are not sure about the type of the value pointing to the content. So first define an untyped pointer (void *p), and then cast the pointer type later.

Exercise 3: Example of using pointers without pointers

#include<iostream>
using namespace std;
int a=10;
double b=3.5;
void *p; 
int main(){
	p = &a;
	cout<< *((int *)p)<<endl;
	p = &b;
	cout<< *((double *)p)<<endl;
	return 0;
}


5. Multiple pointers

There is a question, since pointers can point to other types or cast, then the pointer itself is a type, then who can point to the pointer? Of course it's a 'pointer' of pointers, that is, multiple pointers! ! !

Exercise 4: Example of using double pointers.

#include<iostream>
using namespace std;
int a=10, *p;
int **pp; 
int main(){
	p = &a;
	pp = &p;
	cout<<a<<"="<<*p<<"="<<**pp<<endl;
	return 0;
}


Guess you like

Origin blog.csdn.net/weixin_44775255/article/details/129031168