-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtutorials_first.cu
116 lines (80 loc) · 2.9 KB
/
tutorials_first.cu
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
111
112
/*!
我的教程优势:一系列且强逻辑顺序教程,附有源码,实战性很强。
只有核心cuda处理代码,隐藏在教程中,我将不开源,毕竟已开源很多cuda教程代码,也为本次教程付出很多汗水。
因此,核心代码于yolo部署cuda代码和整个文字解释教程需要有一定补偿,望理解。
可以保证,认真学完教程,cuda编程毫无压力。
详情请链接:http://t.csdn.cn/NaCZ5
@Description : 指针回顾
@Author : tangjun
@Date :
*/
#include <iostream>
#include <time.h>
using namespace std;
/*************************************第一节-指针篇**********************************************/
void Print_Pointers(int* ptr, int N) {
for (int i = 0; i < N; i++)
{
std::cout << "order:\t" << i << "\tptr_value:\t" << *ptr << "\tphysical address:" << ptr << std::endl;
ptr++;
}
}
void pointer_1() {
/* 探索指针赋值方法 */
const int N = 6;
int arr[N];
for (int i = 0; i < N; i++) arr[i] = i + 1; //数组赋值
//指针第一种赋值方法
int* ptr = nullptr;
ptr = arr;
//指针第二种赋值方法
int* ptr2 = arr;
std::cout << "output ptr1 " << std::endl;
Print_Pointers(ptr, N);
std::cout << "\n\noutput ptr2 " << std::endl;
Print_Pointers(ptr2, N);
//单独变量赋值
int a = 20;
int* p = &a;
std::cout << "\n\noutput p value: \t" << *p << "\tphysical address:\t" << p << std::endl;
}
void pointer_2() {
const int N = 6;
int arr[N];
for (int i = 0; i < N; i++) arr[i] = i + 1; //数组赋值
int* ptr = arr; //构建指针
for (int i = 0; i < 5; i++)
{
std::cout << "ptr_value_" << i << ":\t" << *ptr << std::endl;;
ptr++;
}
}
void pointer_3() {
int num = 4;
int* p = #
cout << "*p:\t" << *p << "\t p address:\t" << p << "\tnum value:\t" << num << "\tnum address:\t" << num << endl;
*p = *p + 20; //通过指针更改地址的值
cout << "*p:\t" << *p << "\t p address:\t" << p << "\tnum value:\t" << num << "\tnum address:\t" << num << endl;
num = 30; //更改变量值
cout << "*p:\t" << *p << "\t p address:\t" << p << "\tnum value:\t" << num << "\tnum address:\t" << num << endl;
}
void pointer_4() {
int num = 4;
int* p1 = #
//指针的指针第一种赋值方法
int** p2 = &p1;
//指针的指针第二种赋值方法
int** p3;
p3 = &p1;
cout << "num value:\t" << num << "\t num address:\t" << &num << endl;
cout << "p1 value:\t" << *p1 << "\t p1 address:\t" << p1 << endl;
cout << "p2 value:\t" << *p2 << "\t p2 address:\t" << p2 << endl;
cout << "p3 value:\t" << *p3 << "\t p3 address:\t" << p3 << endl;
cout << "p2 value:\t" << **p2 << "\t p2 address:\t" << *p2 << endl;
}
void main_first() {
pointer_1();
pointer_2();
pointer_3();
pointer_4();
}