-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpointer.cpp
More file actions
110 lines (96 loc) · 2.19 KB
/
pointer.cpp
File metadata and controls
110 lines (96 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
//
// Created by yazhi on 7/27/22.
//
#include <iostream>
#include "pointer.h"
using namespace std;
//值传递
void change1(int n){
cout<<"值传递--函数操作地址"<<&n<<endl; //显示的是拷贝的地址而不是源地址
n++;
}
//引用传递
void change2(int & n){
cout<<"引用传递--函数操作地址"<<&n<<endl;
cout<<"引用传递--n="<<n<<endl;
n++;
}
//指针传递
void change3(int *n){
cout<<"指针传递--函数操作地址 "<<n<<endl;
*n=*n+1;
}
void func(int *p) {
cout << "int func" << endl;
}
void func(char *p) {
cout << "char func" << endl;
}
struct ListNode {
int val;
ListNode* next;
ListNode(): val(0), next(nullptr) {}
ListNode(int x) : val(x), next(nullptr) {}
ListNode(int x, ListNode *next) : val(x), next(next) {}
void printVal()
{
cout << "val=" << val << endl;
}
};
//ListNode a(10);
//ListNode b(20, &a);
//testListNode(&b);
void testListNode(ListNode* head) {
cout << "head=" << head << endl; //head=0x7ff7ba030620
head->printVal();
int c = (*(*head).next).val;
cout << c << endl; //val=20
cout << "*head=" << head->val << endl; //*head=20
}
void using_pointer() {
int i = 12;
int *ip = &i;
cout << i << endl; //12
cout << ip << endl; //0x16f15b68c
cout << *ip << endl; //12,等效于 i
int *p, q;
q = 10;
p = &q;
cout << q << endl;
cout << p << endl;
*p = 100;
cout << q << endl;
cout << " --- a1 ---" << endl;
int a1 = 11;
int a2 = a1;
cout << a2 << endl;
a2 = 33;
cout << a1 << endl;
}
void params_transfer()
{
int n=10;
cout<<"实参的地址"<<&n<<endl;
change1(n);
cout<<"after change1() n="<<n<<endl;
change2(n);
cout<<"after change2() n="<<n<<endl;
change3(&n);
cout<<"after change3() n="<<n<<endl;
}
void main_pointer()
{
// using_pointer();
// func(nullptr); //nullptr 可以转换为任意指针类型和bool
// func(0);
// params_transfer();
// ListNode a(10);
// ListNode b(20, &a);
// testListNode(&b);
auto a = new ListNode(1);
ListNode b(2);
auto b2 = b;
cout << a << endl;
cout << a->val << endl;
// ListNode* test =
}