-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathC9.html
1428 lines (1275 loc) · 141 KB
/
C9.html
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>C进阶语法(九)指针的进阶 | BEIDAO.</title><meta name="keywords" content="C语法,指针"><meta name="author" content="Beidaos"><meta name="copyright" content="Beidaos"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#C6B3B1"><meta name="mobile-web-app-capable" content="yes"><meta name="apple-touch-fullscreen" content="yes"><meta name="apple-mobile-web-app-title" content="C进阶语法(九)指针的进阶"><meta name="application-name" content="C进阶语法(九)指针的进阶"><meta name="apple-mobile-web-app-capable" content="yes"><meta name="apple-mobile-web-app-status-bar-style" content="default"><link rel="bookmark" href="/img/siteicon/apple-touch-icon.png"><link rel="apple-touch-icon-precomposed" sizes="180x180" href="/img/siteicon/apple-touch-icon.png"><link rel="apple-touch-icon" sizes="192x192" href="/img/siteicon/apple-touch-icon.png"><link rel="apple-touch-icon" sizes="512x512" href="/img/siteicon/apple-touch-icon.png"><link rel="apple-touch-startup-image" media="screen and (device-width:320px) and (device-height:568px) and (-webkit-device-pixel-ratio:2) and (orientation:landscape)" href="/img/siteicon/splashIcons/icon_1136x640.png"><link rel="apple-touch-startup-image" media="screen and (device-width:320px) and (device-height:568px) and (-webkit-device-pixel-ratio:2) and (orientation:portrait)" href="/img/siteicon/splashIcons/icon_640x1136.png"><link rel="apple-touch-startup-image" media="screen and (device-width:414px) and (device-height:896px) and (-webkit-device-pixel-ratio:3) and (orientation:landscape)" href="/img/siteicon/splashIcons/icon_2688x1242.png"><link rel="apple-touch-startup-image" media="screen and (device-width:414px) and (device-height:896px) and (-webkit-device-pixel-ratio:2) and (orientation:landscape)" href="/img/siteicon/splashIcons/icon_1792x828.png"><link rel="apple-touch-startup-image" media="screen and (device-width:375px) and (device-height:812px) and (-webkit-device-pixel-ratio:3) and (orientation:portrait)" href="/img/siteicon/splashIcons/icon_1125x2436.png"><link rel="apple-touch-startup-image" media="screen and (device-width:414px) and (device-height:896px) and (-webkit-device-pixel-ratio:2) and (orientation:portrait)" href="/img/siteicon/splashIcons/icon_828x1792.png"><link rel="apple-touch-startup-image" media="screen and (device-width:375px) and (device-height:812px) and (-webkit-device-pixel-ratio:3) and (orientation:landscape)" href="/img/siteicon/splashIcons/icon_2436x1125.png"><link rel="apple-touch-startup-image" media="screen and (device-width:414px) and (device-height:736px) and (-webkit-device-pixel-ratio:3) and (orientation:portrait)" href="/img/siteicon/splashIcons/icon_1242x2208.png"><link rel="apple-touch-startup-image" media="screen and (device-width:414px) and (device-height:736px) and (-webkit-device-pixel-ratio:3) and (orientation:landscape)" href="/img/siteicon/splashIcons/icon_2208x1242.png"><link rel="apple-touch-startup-image" media="screen and (device-width:375px) and (device-height:667px) and (-webkit-device-pixel-ratio:2) and (orientation:landscape)" href="/img/siteicon/splashIcons/icon_1334x750.png"><link rel="apple-touch-startup-image" media="screen and (device-width:375px) and (device-height:667px) and (-webkit-device-pixel-ratio:2) and (orientation:portrait)" href="/img/siteicon/splashIcons/icon_750x1334.png"><link rel="apple-touch-startup-image" media="screen and (device-width:1024px) and (device-height:1366px) and (-webkit-device-pixel-ratio:2) and (orientation:landscape)" href="/img/siteicon/splashIcons/icon_2732x2048.png"><meta name="description" content="指针的进阶">
<meta property="og:type" content="article">
<meta property="og:title" content="C进阶语法(九)指针的进阶">
<meta property="og:url" content="https://www.beidaoaz.top/C9.html">
<meta property="og:site_name" content="BEIDAO.">
<meta property="og:description" content="指针的进阶">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://cdn.statically.io/gh/AnZhiJJ/Blog_Img@main/%E5%8D%9A%E5%AE%A2%E7%AB%99%E7%82%B9%E9%85%8D%E5%9B%BE/image.3qlx7labk120.png">
<meta property="article:published_time" content="2023-04-22T12:56:17.000Z">
<meta property="article:modified_time" content="2024-03-01T07:09:27.952Z">
<meta property="article:author" content="Beidaos">
<meta property="article:tag" content="C语法">
<meta property="article:tag" content="指针">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://cdn.statically.io/gh/AnZhiJJ/Blog_Img@main/%E5%8D%9A%E5%AE%A2%E7%AB%99%E7%82%B9%E9%85%8D%E5%9B%BE/image.3qlx7labk120.png"><link rel="shortcut icon" href="/private_img/web_logo.png"><link rel="canonical" href="https://www.beidaoaz.top/C9"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//npm.elemecdn.com"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><meta name="google-site-verification" content="xxx"/><meta name="baidu-site-verification" content="code-xxx"/><meta name="msvalidate.01" content="xxx"/><link rel="manifest" href="/manifest.json"/><meta name="msapplication-TileColor" content="var(--anzhiyu-main)"/><link rel="apple-touch-icon" sizes="180x180" href="/img/siteicon/128.png"/><link rel="icon" type="image/png" sizes="32x32" href="/img/siteicon/32.png"/><link rel="icon" type="image/png" sizes="16x16" href="/img/siteicon/16.png"/><link rel="mask-icon" href="/img/siteicon/128.png" color="#5bbad5"/><script>if ('serviceWorker' in navigator) {
if (navigator.serviceWorker.controller) {
navigator.serviceWorker.addEventListener('controllerchange', function() {
location.reload()
})
}
window.addEventListener('load', function() {
navigator.serviceWorker.register('/service-worker.js')
})
}</script><link rel="stylesheet" href="css/index.css"><link rel="stylesheet" href="https://npm.elemecdn.com/[email protected]/dist/snackbar.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://npm.elemecdn.com/@fancyapps/[email protected]/dist/fancybox.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = {
root: '/',
friends_vue_info: undefined,
navMusic: true,
algolia: undefined,
localSearch: {"path":"/search.xml","preload":true,"languages":{"hits_empty":"找不到您查询的内容:${query}"}},
translate: {"defaultEncoding":2,"translateDelay":0,"msgToTraditionalChinese":"繁","msgToSimplifiedChinese":"简","rightMenuMsgToTraditionalChinese":"转为繁体","rightMenuMsgToSimplifiedChinese":"转为简体"},
noticeOutdate: {"limitDay":30,"position":"top","messagePrev":"本文上次更新距离今天已经过去","messageNext":"天, 文中内容可能已经过时,望周知。"},
highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":330},
copy: {
success: '复制成功',
error: '复制错误',
noSupport: '浏览器不支持'
},
relativeDate: {
homepage: false,
post: false
},
runtime: '天',
date_suffix: {
just: '刚刚',
min: '分钟前',
hour: '小时前',
day: '天前',
month: '个月前'
},
copyright: undefined,
lightbox: 'fancybox',
Snackbar: {"chs_to_cht":"你已切换为繁体","cht_to_chs":"你已切换为简体","day_to_night":"你已切换为深色模式","night_to_day":"你已切换为浅色模式","bgLight":"#3b70fc","bgDark":"#1f1f1f","position":"top-center"},
source: {
justifiedGallery: {
js: 'https://npm.elemecdn.com/[email protected]/dist/fjGallery.min.js',
css: 'https://npm.elemecdn.com/[email protected]/dist/fjGallery.css'
}
},
isPhotoFigcaption: false,
islazyload: true,
isAnchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
title: 'C进阶语法(九)指针的进阶',
isPost: true,
isHome: false,
isHighlightShrink: false,
isToc: true,
postUpdate: '2024-03-01 15:09:27'
}</script><noscript><style type="text/css">
#nav {
opacity: 1
}
.justified-gallery img {
opacity: 1
}
#recent-posts time,
#post-meta time {
display: inline !important
}
</style></noscript><script>(win=>{
win.saveToLocal = {
set: function setWithExpiry(key, value, ttl) {
if (ttl === 0) return
const now = new Date()
const expiryDay = ttl * 86400000
const item = {
value: value,
expiry: now.getTime() + expiryDay,
}
localStorage.setItem(key, JSON.stringify(item))
},
get: function getWithExpiry(key) {
const itemStr = localStorage.getItem(key)
if (!itemStr) {
return undefined
}
const item = JSON.parse(itemStr)
const now = new Date()
if (now.getTime() > item.expiry) {
localStorage.removeItem(key)
return undefined
}
return item.value
}
}
win.getScript = url => new Promise((resolve, reject) => {
const script = document.createElement('script')
script.src = url
script.async = true
script.onerror = reject
script.onload = script.onreadystatechange = function() {
const loadState = this.readyState
if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
script.onload = script.onreadystatechange = null
resolve()
}
document.head.appendChild(script)
})
win.getCSS = (url,id = false) => new Promise((resolve, reject) => {
const link = document.createElement('link')
link.rel = 'stylesheet'
link.href = url
if (id) link.id = id
link.onerror = reject
link.onload = link.onreadystatechange = function() {
const loadState = this.readyState
if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
link.onload = link.onreadystatechange = null
resolve()
}
document.head.appendChild(link)
})
win.activateDarkMode = function () {
document.documentElement.setAttribute('data-theme', 'dark')
if (document.querySelector('meta[name="theme-color"]') !== null) {
document.querySelector('meta[name="theme-color"]').setAttribute('content', '#18171d')
}
}
win.activateLightMode = function () {
document.documentElement.setAttribute('data-theme', 'light')
if (document.querySelector('meta[name="theme-color"]') !== null) {
document.querySelector('meta[name="theme-color"]').setAttribute('content', '#C6B3B1')
}
}
const t = saveToLocal.get('theme')
const now = new Date()
const hour = now.getHours()
const isNight = hour <= 8 || hour >= 22
if (t === undefined) isNight ? activateDarkMode() : activateLightMode()
else if (t === 'light') activateLightMode()
else activateDarkMode()
const asideStatus = saveToLocal.get('aside-status')
if (asideStatus !== undefined) {
if (asideStatus === 'hide') {
document.documentElement.classList.add('hide-aside')
} else {
document.documentElement.classList.remove('hide-aside')
}
}
const detectApple = () => {
if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
document.documentElement.classList.add('apple')
}
}
detectApple()
})(window)</script><link rel="preconnect" href="https://fonts.googleapis.com"><link rel="preconnect" href="https://fonts.gstatic.com" crossorigin><link href="https://fonts.loli.net/css2?family=Noto+Serif+SC:wght@400;500;700&display=swap" rel="stylesheet"><link href="https://fonts.loli.net/css2?family=Ma+Shan+Zheng:wght@400;500;700&display=swap" rel="stylesheet"><link rel="stylesheet" href="/css/custom.css" media="defer" onload="this.media='all'"><link rel="stylesheet" href="/css/background-page.css?1"><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/zhheo/JS-Heo@main/tag-link/tag-link.css"><!-- hexo injector head_end start -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.css">
<link rel="stylesheet" href="https://cdn.cbd.int/hexo-butterfly-clock-anzhiyu/lib/clock.min.css" /><link rel="stylesheet" href="https://cdn.cbd.int/hexo-butterfly-tag-plugins-plus@latest/lib/assets/font-awesome-animation.min.css" media="defer" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.cbd.int/hexo-butterfly-tag-plugins-plus@latest/lib/tag_plugins.css" media="defer" onload="this.media='all'"><script src="https://cdn.cbd.int/hexo-butterfly-tag-plugins-plus@latest/lib/assets/carousel-touch.js"></script><!-- hexo injector head_end end --><meta name="generator" content="Hexo 6.3.0"></head><body data-type="anzhiyu"><div id="web_bg"></div><div id="an_music_bg"></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header"><nav id="nav"><span id="blog_name"><div class="back-home-button" tabindex="-1"><i class="anzhiyufont anzhiyu-icon-grip-vertical"></i><div class="back-menu-list-groups"><div class="back-menu-list-group"><div class="back-menu-list-title">学术</div><div class="back-menu-list"><a class="back-menu-item" href="https://sci-hub.st/" rel="external nofollow noreferrer" title="Sci-hub" target="_blank"><img class="back-menu-item-icon" src="https://cdn.staticaly.com/gh/AnZhiJJ/Blog_Img@main/图标/ravenround_hs.f9qogiemdnk.gif" alt="Sci-hub"/><span class="back-menu-item-text">Sci-hub</span></a><a class="back-menu-item" href="https://www.connectedpapers.com/" rel="external nofollow noreferrer" title="ConnetPapers" target="_blank"><img class="back-menu-item-icon" src="https://cdn.staticaly.com/gh/AnZhiJJ/Blog_Img@main/图标/Connected Papers.avesmhofaig.jpg" alt="ConnetPapers"/><span class="back-menu-item-text">ConnetPapers</span></a></div></div><div class="back-menu-list-group"><div class="back-menu-list-title">工具</div><div class="back-menu-list"><a class="back-menu-item" href="https://vercel.com/" rel="external nofollow noreferrer" title="Vercel" target="_blank"><img class="back-menu-item-icon" src="https://cdn.staticaly.com/gh/AnZhiJJ/Blog_Img@main/图标/vercel.7910zc14sj40.png" alt="Vercel"/><span class="back-menu-item-text">Vercel</span></a><a class="back-menu-item" href="https://picx.xpoet.cn/" rel="external nofollow noreferrer" title="PicX" target="_blank"><img class="back-menu-item-icon" src="https://cdn.staticaly.com/gh/AnZhiJJ/Blog_Img@main/图标/PicX.1erb7rkva5c0.ico" alt="PicX"/><span class="back-menu-item-text">PicX</span></a></div></div><div class="back-menu-list-group"><div class="back-menu-list-title">网页</div><div class="back-menu-list"><a class="back-menu-item" href="https://www.bilibili.com/" rel="external nofollow noreferrer" title="BiliBili" target="_blank"><img class="back-menu-item-icon" src="https://cdn.staticaly.com/gh/AnZhiJJ/Blog_Img@main/图标/bilibili.7fu3r06fhp80.svg" alt="BiliBili"/><span class="back-menu-item-text">BiliBili</span></a></div></div><div class="back-menu-list-group"><div class="back-menu-list-title">镜像站</div><div class="back-menu-list"><a class="back-menu-item" href="https://mirrors.tuna.tsinghua.edu.cn/" rel="external nofollow noreferrer" title="清华镜像站" target="_blank"><img class="back-menu-item-icon" src="https://cdn.staticaly.com/gh/AnZhiJJ/Blog_Img@main/图标/logo-small.4bmf6842eso0.png" alt="清华镜像站"/><span class="back-menu-item-text">清华镜像站</span></a><a class="back-menu-item" href="https://developer.aliyun.com/mirror/?spm=a2c6h.25603864.0.0.7cff28b9BKDFoW" rel="external nofollow noreferrer" title="阿里云镜像站" target="_blank"><img class="back-menu-item-icon" src="https://cdn.staticaly.com/gh/AnZhiJJ/Blog_Img@main/图标/阿里云.3j8goscmtmu0.svg" alt="阿里云镜像站"/><span class="back-menu-item-text">阿里云镜像站</span></a></div></div></div></div><a id="site-name" href="/index.html"><div class="title">BEIDAO.</div><i class="anzhiyufont anzhiyu-icon-house-chimney"></i></a></span><div class="mask-name-container"><center id="name-container"><a id="page-name" href="javascript:anzhiyu.scrollToDest(0, 500)" rel="external nofollow noreferrer">PAGE_NAME</a></center></div><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page faa-parent animated-hover" href="javascript:void(0);" rel="external nofollow noreferrer"><span> 文章</span></a><ul class="menus_item_child" style="left:-79px;"><li><a class="site-page child faa-parent animated-hover" href="/archives/"><i class="anzhiyufont anzhiyu-icon-box-archive faa-tada" style="font-size: 0.9em;"></i><span> 归档</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/categories/"><i class="anzhiyufont anzhiyu-icon-shapes faa-tada" style="font-size: 0.9em;"></i><span> 分类</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/tags/"><svg class="icon faa-tada" aria-hidden="true"><use xlink:href="#icon-biaoqiantags3"></use></svg><span> 标签</span></a></li></ul></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="javascript:void(0);" rel="external nofollow noreferrer"><span> 社交</span></a><ul class="menus_item_child" style="left:-31px;"><li><a class="site-page child faa-parent animated-hover" href="/link/"><i class="anzhiyufont anzhiyu-icon-link faa-tada" style="font-size: 0.9em;"></i><span> 友链</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/essay/"><i class="anzhiyufont anzhiyu-icon-lightbulb faa-tada" style="font-size: 0.9em;"></i><span> 闲言碎语</span></a></li></ul></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="javascript:void(0);" rel="external nofollow noreferrer"><span> 我的</span></a><ul class="menus_item_child" style="left:-79px;"><li><a class="site-page child faa-parent animated-hover" href="/music/?id=886726002&server=netease"><i class="anzhiyufont anzhiyu-icon-music faa-tada" style="font-size: 0.9em;"></i><span> 音乐馆</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/collect/"><svg class="icon faa-tada" aria-hidden="true"><use xlink:href="#icon-dropboxdropbox4"></use></svg><span> 藏宝阁</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/album/"><i class="anzhiyufont anzhiyu-icon-images faa-tada" style="font-size: 0.9em;"></i><span> 相册集</span></a></li></ul></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="javascript:void(0);" rel="external nofollow noreferrer"><span> 关于</span></a><ul class="menus_item_child" style="left:17px;"><li><a class="site-page child faa-parent animated-hover" href="/about/"><i class="anzhiyufont anzhiyu-icon-paper-plane faa-tada" style="font-size: 0.9em;"></i><span> 关于博主</span></a></li></ul></div></div></div><div id="nav-right"><div class="nav-button" id="randomPost_button"><a class="site-page" onclick="toRandomPost()" title="随机前往一个文章" href="javascript:void(0);" rel="external nofollow noreferrer"><i class="anzhiyufont anzhiyu-icon-dice"></i></a></div><div class="nav-button" id="search-button"><a class="site-page social-icon search" href="javascript:void(0);" rel="external nofollow noreferrer" title="搜索内容 - 搜一搜,省时又省力"><i class="anzhiyufont anzhiyu-icon-magnifying-glass"></i><span> 搜索</span></a></div><div class="nav-button" id="changeBg-button"><a class="site-page social-icon search" href="javascript:void(0);" rel="external nofollow noreferrer" onclick="toggleWinbox()" title="切换背景 - 换一个背景,换一种感觉"><i class="iconfont icon-qiehuankapian"></i></a></div><div class="nav-button" id="darkmode_navswitch"><a class="darkmode_switchbutton" type="button" href="javascript:void(0);" rel="external nofollow noreferrer" title="浅色和深色模式转换" onclick="rm.switchDarkMode()"><i class="anzhiyufont anzhiyu-icon-circle-half-stroke" style="font-size: 1.3rem"></i></a></div><div class="nav-button" id="nav-totop"><a class="totopbtn" href="javascript:void(0);" rel="external nofollow noreferrer"><i class="anzhiyufont anzhiyu-icon-arrow-up"></i><span id="percent" onclick="anzhiyu.scrollToDest(0,500)">0</span></a></div><div id="toggle-menu"><a class="site-page" href="javascript:void(0);" rel="external nofollow noreferrer"><i class="anzhiyufont anzhiyu-icon-bars"></i></a></div></div></nav><div id="post-info"><div id="post-firstinfo"><div class="meta-firstline"><a class="post-meta-original">原创</a><span class="post-meta-categories"><span class="post-meta-separator"></span><i class="anzhiyufont anzhiyu-icon-inbox post-meta-icon"></i><a class="post-meta-categories" href="categories/C%E8%AF%AD%E6%B3%95/">C语法</a></span><span class="article-meta tags"><a class="article-meta__tags" href="tags/C%E8%AF%AD%E6%B3%95/"><span><i class="iconfont icon-Tags"></i>C语法</span></a><a class="article-meta__tags" href="tags/%E6%8C%87%E9%92%88/"><span><i class="iconfont icon-Tags"></i>指针</span></a></span></div></div><h1 class="post-title">C进阶语法(九)指针的进阶</h1><div id="post-meta"><div class="meta-firstline"><i class="iconfont icon-zuozhe"></i><span class="post-meta-author">安志</span><span class="post-meta-date"><span class="post-meta-separator"></span><i class="anzhiyufont anzhiyu-icon-calendar-days post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2023-04-22T12:56:17.000Z" title="发表于 2023-04-22 20:56:17">2023-04-22</time><span class="post-meta-separator"></span><i class="anzhiyufont anzhiyu-icon-history post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2024-03-01T07:09:27.952Z" title="更新于 2024-03-01 15:09:27">2024-03-01</time></span></div><div class="meta-secondline"><span class="post-meta-separator"></span><span class="post-meta-wordcount"><i class="anzhiyufont anzhiyu-icon-file-word post-meta-icon" title="文章字数"></i><span class="post-meta-label" title="文章字数">字数总计:</span><span class="word-count" title="文章字数">3.6k</span><span class="post-meta-separator"></span><i class="anzhiyufont anzhiyu-icon-clock post-meta-icon" title="阅读时长"></i><span class="post-meta-label" title="阅读时长">阅读时长:</span><span>14分钟</span></span><span class="post-meta-separator"></span><span class="post-meta-position" title="作者IP属地为北京"><i class="anzhiyufont anzhiyu-icon-location-dot"></i>北京</span></div></div></div><section class="main-hero-waves-area waves-area"><svg class="waves-svg" xmlns="http://www.w3.org/2000/svg" xlink="http://www.w3.org/1999/xlink" viewBox="0 24 150 28" preserveAspectRatio="none" shape-rendering="auto"><defs><path id="gentle-wave" d="M -160 44 c 30 0 58 -18 88 -18 s 58 18 88 18 s 58 -18 88 -18 s 58 18 88 18 v 44 h -352 Z"></path></defs><g class="parallax"><use href="#gentle-wave" x="48" y="0"></use><use href="#gentle-wave" x="48" y="3"></use><use href="#gentle-wave" x="48" y="5"></use><use href="#gentle-wave" x="48" y="7"></use></g></svg></section><div id="post-top-cover"><img class="nolazyload" id="post-top-bg" src="https://cdn.statically.io/gh/AnZhiJJ/Blog_Img@main/博客站点配图/image.3qlx7labk120.png"></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h2 id="指针基础">指针基础</h2>
<p>指针的主题,我们在初级阶段的《指针》章节已经接触过了,我们知道了指针的概念:</p>
<blockquote>
<p>1.指针就是变量,是用来存地址的,地址是唯一标识一块内存空间<br>
2.指针的大小是固定的4/8个字节(由平台决定,32位/64位)<br>
3.指针是有类型的,通常类型有char、int 、short、long、float、double<br>
4.指针类型决定了指针±整数的步长,指针解引用操作的时候的能够访问空间的大小<br>
5.指针的运算。</p>
<ul class="lvl-1">
<li class="lvl-2"><strong>指针±整数(==>指针指向的是该地址的上一个或下一个地址)</strong></li>
<li class="lvl-2"><strong>指针-指针(指向同一内存空间,可得指针之间的元素个数)</strong></li>
<li class="lvl-2"><strong>指针的关系运算(比较地址大小)</strong></li>
</ul>
</blockquote>
<h2 id="字符指针">字符指针</h2>
<p>指针类型中存在一种字符指针<code>char*</code></p>
<p><b>一般使用:</b></p>
<figure class="highlight c"><table><tr><td class="code"><pre><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="type">char</span> ch= <span class="string">'w'</span>;</span><br><span class="line"> <span class="type">char</span> *pc = &ch;</span><br><span class="line"> *pc =<span class="string">'w'</span>;</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p><b>另一种使用方式:</b></p>
<figure class="highlight c"><table><tr><td class="code"><pre><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="type">const</span> <span class="type">char</span>* pstr = <span class="string">"hello bit."</span>;<span class="comment">//这里是把一个字符串放到pstr指针变量里了吗?</span></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%s\n"</span>, pstr);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p><em><strong>注:</strong></em></p>
<blockquote>
<p><strong><font color=black>(一)常量指针int const* p 和 const int* p</font></strong></p>
<p><b><font color=red>const 放在指针变量的 * 左边时,表示该指针所指向的数据(地址内的内容)是常量,即不能通过该指针修改所指向的数据。但p所指向的地址可以变。</font></b></p>
<figure class="highlight c"><table><tr><td class="code"><pre><span class="line">><span class="type">int</span> num = <span class="number">10</span>;</span><br><span class="line">><span class="type">const</span> <span class="type">int</span> *p = &num; <span class="comment">// 将const放在*的左边,表示所指向的数据是常量</span></span><br><span class="line">>*p = <span class="number">20</span>; <span class="comment">// 编译错误,因为p所指向的数据是常量</span></span><br><span class="line">>p++; <span class="comment">// 合法,p本身不是常量</span></span><br></pre></td></tr></table></figure>
<p>在上面的例子中,p 是一个指向 num的常量指针,即不能通过 p修改 num的值。但是可以通过 p 修改其指向的地址。</p>
<p>注**:int const* p定义了一个指向 const int类型数据的指针变量 p,也就是说,p 所指向的数据是一个常量,不能通过 p修改其所指向的数据**。但是 p本身不是常量,可以指向其他的 const int类型数据。</p>
<p><strong><font color=black>(二)指针常量int* const p</font></strong></p>
<p><b><font color=red>const放在指针变量的*右边时,表示指针本身是常量。它指向的地址是不可改变的,但地址里的内容可以通过指针改变。</font></b></p>
<figure class="highlight c"><table><tr><td class="code"><pre><span class="line">><span class="type">int</span> num = <span class="number">10</span>;</span><br><span class="line">><span class="type">int</span> *<span class="type">const</span> p = &num; <span class="comment">// 将const放在*的左边,并且指针本身也是常量</span></span><br><span class="line">>*p = <span class="number">20</span>; <span class="comment">// 合法,可以通过p来修改所指向的数据</span></span><br><span class="line">>p++; <span class="comment">// 编译错误,因为p本身是常量,不能修改其指向的地址</span></span><br></pre></td></tr></table></figure>
<p>在上面的例子中,int* const p就是一个指针常量,表示定义了一个指向 int 类型的指针变量 p,且这个指针变量是一个常量,其值(地址)不能被改变,但它所指向的 int类型数据(地址内的内容)可以被修改。</p>
<p><b><font color=black>总结:</font></b></p>
<p>一般是根据靠近原则来看,<strong>const修饰p那么就是指针本身值(即指向的地址)不变,const修饰*p那么就是指指针指向的变量值(指向的值)不变</strong></p>
<p><strong><font color=red>加深记忆记住三句话:</font></strong></p>
<ul class="lvl-1">
<li class="lvl-2">
<p>指针和 const 谁在前先读谁 ;</p>
</li>
<li class="lvl-2">
<p>*象征着地址,const象征着内容;</p>
</li>
<li class="lvl-2">
<p>谁在前面<strong>谁就不允许改变。</strong></p>
</li>
</ul>
</blockquote>
<figure class="highlight c"><table><tr><td class="code"><pre><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="type">char</span> arr1[] = <span class="string">"abcdefk"</span>;</span><br><span class="line"> <span class="type">char</span> arr2[] = <span class="string">"abcdefk"</span>;</span><br><span class="line"> <span class="type">const</span> <span class="type">char</span>* p1 = <span class="string">"abcdefk"</span>;<span class="comment">//const使变量</span></span><br><span class="line"> <span class="type">const</span> <span class="type">char</span>* p2 = <span class="string">"abcdefk"</span>;<span class="comment">//常量字符串的地址会在内存中单独开辟一个空间,地址固定,</span></span><br><span class="line"> <span class="keyword">if</span> (p1 == p2)</span><br><span class="line"> {</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"hehe\n"</span>);</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> {</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"haha\n "</span>)</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//if (arr1 = arr2)</span></span><br><span class="line"> <span class="comment">//{</span></span><br><span class="line"> <span class="comment">// printf("hehe\n");</span></span><br><span class="line"> <span class="comment">//}</span></span><br><span class="line"> <span class="comment">//else</span></span><br><span class="line"> <span class="comment">//{</span></span><br><span class="line"> <span class="comment">// printf("haha\n");</span></span><br><span class="line"> <span class="comment">//}</span></span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p>这里p1和p2指向的是一个<b><font color=red>同一个常量字符串</font></b>。C/C++会把常量字符串存储到单独的一个内存区域,当几个指针。<strong>指向同一个字符串的时候,他们实际会指向同一块内存。但是用相同的常量字符串去初始化</strong><br>
<strong>不同的数组的时候就会开辟出不同的内存块</strong>。所以arr11和arr2不同,p1和p2不同。 <strong>来源《剑指offer》</strong></p>
<h2 id="指针数组">指针数组</h2>
<blockquote>
<p><strong>预备知识:</strong></p>
<ul class="lvl-1">
<li class="lvl-2">
<p>1.&arr-数组名-此时数组名不是首元素的地址–数组名表示整个数组–&数组名 取出的是整个数组的地址。</p>
</li>
<li class="lvl-2">
<p>2.sizeof(arr)-sizeof(数组名)-数组名表示整个数组-sizeof(数组名)计算的是整个数组的大小。</p>
</li>
<li class="lvl-2">
<p>除1、2以外,<strong><font color=blue>数组名表示数组的首元素的地址</font></strong></p>
</li>
</ul>
<figure class="highlight c"><table><tr><td class="code"><pre><span class="line">><span class="type">int</span>* arr1[<span class="number">10</span>]; <span class="comment">//整形指针的数组</span></span><br><span class="line"><span class="type">char</span> *arr2[<span class="number">4</span>]; <span class="comment">//一级字符指针的数组</span></span><br><span class="line"><span class="type">char</span> **arr3[<span class="number">5</span>];<span class="comment">//二级字符指针的数组```</span></span><br><span class="line"></span><br><span class="line">```c</span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"><span class="type">int</span> arr1[] = { <span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span> };</span><br><span class="line"><span class="type">int</span> arr2[] = { <span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span> };</span><br><span class="line"><span class="type">int</span> arr3[] = { <span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span>,<span class="number">7</span> };</span><br><span class="line"><span class="type">int</span>* parr[] = { arr1,arr2,arr3 };</span><br><span class="line"><span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
</blockquote>
<p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-lazy-src="https://cdn.statically.io/gh/AnZhiJJ/Blog_Img@C/%E7%AC%AC%E4%B9%9D%E8%8A%82%20%E6%8C%87%E9%92%88%E7%9A%84%E8%BF%9B%E9%98%B6/image-20230422002000521.png" alt="image-20230422002000521"></p>
<p><strong>指针数组用法:</strong></p>
<figure class="highlight c"><table><tr><td class="code"><pre><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="type">int</span> arr1[] = { <span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span> };</span><br><span class="line"> <span class="type">int</span> arr2[] = { <span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span> };</span><br><span class="line"> <span class="type">int</span> arr3[] = { <span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span>,<span class="number">7</span> };</span><br><span class="line"></span><br><span class="line"> <span class="type">int</span>* parr[] = { arr1,arr2,arr3 };</span><br><span class="line"> <span class="type">int</span> i = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">for</span> (i = <span class="number">0</span>;i < <span class="number">3</span>;i++)</span><br><span class="line"> {</span><br><span class="line"> <span class="type">int</span> j = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">for</span> (j = <span class="number">0</span>;j < <span class="number">5</span>;j++)</span><br><span class="line"> {</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d "</span>, *(parr[i] + j));</span><br><span class="line"> }</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"\n"</span>);</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<h2 id="数组指针">数组指针</h2>
<h3 id="数组指针的定义">数组指针的定义</h3>
<p><strong>数组指针是指针</strong></p>
<blockquote>
<p>整形指针:int * pint; 能够指向整形数据的指针。浮点型指针:float * pf; 能够指向浮点型数据的指针。数组指针:能够指向数组的指针</p>
</blockquote>
<figure class="highlight c"><table><tr><td class="code"><pre><span class="line"><span class="type">int</span> *p1[<span class="number">10</span>];<span class="comment">//==int* p1[10] 指针数组</span></span><br><span class="line"><span class="type">int</span> (*p2)[<span class="number">10</span>];<span class="comment">//数组指针</span></span><br><span class="line"><span class="comment">//p1, p2分别是什么?</span></span><br></pre></td></tr></table></figure>
<p><strong>指针数组</strong>,对于语句“int* p1[10]”,因为“[]”的优先级要比“*”要高,所以 p1 先与“[]”结合,构成一个数组的定义,数组名为 p1,而“int*”修饰的是数组的内容,即数组的每个元素。也就是说,<strong>该数组包含 10个指向 int 类型数据的指针,<strong>如图 1 所示,因此,它是</strong>一个指针数组</strong>。</p>
<p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-lazy-src="https://cdn.statically.io/gh/AnZhiJJ/Blog_Img@C/%E7%AC%AC%E4%B9%9D%E8%8A%82%20%E6%8C%87%E9%92%88%E7%9A%84%E8%BF%9B%E9%98%B6/22201218-19ddd896ae6a45e1b61c7e3ee59b4662.jpg" alt="Attachment"></p>
<p> 图 1</p>
<p><strong>数组指针</strong>,对于语句“int(*p2)[5]”,“()”的优先级比“[]”高,“*<strong>”号和 p2 构成一个指针的定义</strong>,<strong>指针变量名为 p2</strong>,**而 int 修饰的是数组的内容,即数组的每个元素。**也就是说,<strong>p2 是一个指针,它指向一个包含 10 个 int 类型数据的数组</strong>,如图 2 所示。很显然,它是一个数组指针,<strong>数组在这里并没有名字,是个匿名数组</strong>。</p>
<p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-lazy-src="https://cdn.statically.io/gh/AnZhiJJ/Blog_Img@C/%E7%AC%AC%E4%B9%9D%E8%8A%82%20%E6%8C%87%E9%92%88%E7%9A%84%E8%BF%9B%E9%98%B6/22201218-19ddd896ae6a45e1b61c7e3ee59b4662(1).jpg" alt="Attachment"></p>
<p> 图 2</p>
<p>由此可见,</p>
<blockquote>
<p>对指针数组来说,首先它是一个数组,数组的元素都是指针,也就是说该数组存储的是指针,数组占多少个字节由数组本身决定;而对数组指针来说,<strong>首先它是一个指针</strong>,**它指向一个数组,**也就是说它是指向数组的指针,>在 32 位系统下永远占 4 字节,至于它指向的数组占多少字节,这个不能够确定,要看具体情况。</p>
</blockquote>
<figure class="highlight c"><table><tr><td class="code"><pre><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="comment">//int* p = NULL;//p是整形指针 - 指向整形的指针 - 可以存放整形的地址</span></span><br><span class="line"> <span class="comment">//char* pc = NULL;//pa是字符指针 - 指向字符的指针 - 可以存放字符的地址</span></span><br><span class="line"> <span class="comment">//数组指针 - 指向数组的指针 -可以存放数组的地址</span></span><br><span class="line"> <span class="comment">//int arr[10] = { 0 };</span></span><br><span class="line"> <span class="comment">//arr-首元素地址</span></span><br><span class="line"> <span class="comment">//&arr[0]-首元素的地址</span></span><br><span class="line"> <span class="comment">//&arr-数组的地址(整个数组)</span></span><br><span class="line"></span><br><span class="line"> <span class="type">int</span> arr[<span class="number">10</span>] = { <span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span>,<span class="number">7</span>,<span class="number">8</span>,<span class="number">9</span>,<span class="number">10</span> };</span><br><span class="line"> <span class="type">int</span> (*p)[<span class="number">10</span>] = &arr;<span class="comment">//数组的地址要存起来</span></span><br><span class="line"> <span class="comment">//上面的p就是数组指针</span></span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p><strong>例:</strong></p>
<figure class="highlight c"><table><tr><td class="code"><pre><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="type">char</span>* arr[<span class="number">5</span>];<span class="comment">//指针数组</span></span><br><span class="line"> <span class="type">char</span>* (*pa)[<span class="number">5</span>]=&arr;</span><br><span class="line"> <span class="comment">//*pa指针指向数组5个元素,即(*pa)[5];而指向的数组元素类型是char*。</span></span><br><span class="line"> <span class="comment">//故取数组的地址&arr写成:char* (*pa)[5]=&arr; </span></span><br><span class="line"></span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-lazy-src="https://cdn.statically.io/gh/AnZhiJJ/Blog_Img@C/%E7%AC%AC%E4%B9%9D%E8%8A%82%20%E6%8C%87%E9%92%88%E7%9A%84%E8%BF%9B%E9%98%B6/image-20230422185558143.png" alt="image-20230422185558143" style="zoom:50%;" />
<h3 id="数组名VS数组名">&数组名VS数组名</h3>
<p><strong><font color=red>&arr是取的整个数组的地址</font></strong></p>
<h3 id="数组指针用法">数组指针用法</h3>
<h4 id="用法1:">用法1:</h4>
<figure class="highlight c"><table><tr><td class="code"><pre><span class="line"><span class="type">int</span>(*pa)[<span class="number">10</span>] = &arr;</span><br><span class="line"><span class="type">int</span> i = <span class="number">0</span>;</span><br><span class="line"><span class="keyword">for</span> (i = <span class="number">0</span>;i < <span class="number">10</span>;i++)</span><br><span class="line">{</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d "</span>, *(*pa+i));<span class="comment">//*pa==arr</span></span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<h4 id="用法2-地址传参">用法2:地址传参</h4>
<p><strong>二维数组看作一维数组,则其数组名就是首元素的地址</strong></p>
<figure class="highlight c"><table><tr><td class="code"><pre><span class="line">arr[i] == *(a+i) == *(p+i) == p[i]</span><br><span class="line"> </span><br><span class="line">*(*(p + i) + j)== *(p[i] + j)==(*(p + i))[j]==p[i][j]</span><br></pre></td></tr></table></figure>
<p><strong>例:</strong></p>
<figure class="highlight c"><table><tr><td class="code"><pre><span class="line">(*(p + i) + j);<span class="comment">//(p + i)找到n维数组第i行的地址,(*(p + i) + j)找到n维数组第i行的第j列元素的地址</span></span><br></pre></td></tr></table></figure>
<figure class="highlight c"><table><tr><td class="code"><pre><span class="line"><span class="comment">// 参数是数组的形式</span></span><br><span class="line"><span class="comment">//void print1(int arr[3][5], int x, int y)</span></span><br><span class="line"><span class="comment">//{</span></span><br><span class="line"><span class="comment">// //int i = 0, j = 0;</span></span><br><span class="line"><span class="comment">// //for (i = 0;i < x;i++)</span></span><br><span class="line"><span class="comment">// //{</span></span><br><span class="line"><span class="comment">// // for (j = 0;j < y;j++)</span></span><br><span class="line"><span class="comment">// // {</span></span><br><span class="line"><span class="comment">// // printf("%d ", arr[i][j]);</span></span><br><span class="line"><span class="comment">// // }</span></span><br><span class="line"><span class="comment">// // printf("\n");</span></span><br><span class="line"><span class="comment">// //}</span></span><br><span class="line"><span class="comment">//</span></span><br><span class="line"><span class="comment">// return 0;</span></span><br><span class="line"><span class="comment">//}</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//参数是指针的形式</span></span><br><span class="line"><span class="type">void</span> <span class="title function_">print2</span><span class="params">(<span class="type">int</span> (*p)[<span class="number">5</span>], <span class="type">int</span> x, <span class="type">int</span> y)</span><span class="comment">//int (*p)[5] - 数组指针p是指向含5个元素为int类型的数组</span></span><br><span class="line">{</span><br><span class="line"> <span class="type">int</span> i = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">for</span> (i = <span class="number">0</span>;i < x;i++)</span><br><span class="line"> {</span><br><span class="line"> <span class="type">int</span> j = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">for</span> (j = <span class="number">0</span>;j < y;j++)</span><br><span class="line"> {</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d "</span>, p[i][j]);</span><br><span class="line"> <span class="comment">// printf("%d ", *(p[i] + j));</span></span><br><span class="line"> <span class="comment">//printf("%d ", * (*(p + i) + j));//(p + i)找到n维数组第i行的地址,(*(p + i) + j)找到n维数组第i行的第j列元素的地址</span></span><br><span class="line"> <span class="comment">//printf("%d ", (*(p + i))[j]);</span></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"\n"</span>);</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="type">int</span> arr[<span class="number">3</span>][<span class="number">5</span>] = { {<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>},{<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span>},{<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span>,<span class="number">7</span>} };</span><br><span class="line"> <span class="comment">//print1(arr, 3, 5);//arr - 数组名 -数组名就是首元素的地址</span></span><br><span class="line"> print2(arr, <span class="number">3</span>, <span class="number">5</span>);</span><br><span class="line"> <span class="comment">//数组名arr,表示首元素的地址</span></span><br><span class="line"> <span class="comment">//但是二维数组的首元素是二维数组的第一行</span></span><br><span class="line"> <span class="comment">//所以这里传递的arr,其实相当于第一行的地址,是一维数组的地址</span></span><br><span class="line"> <span class="comment">//可以数组指针来接收</span></span><br><span class="line"> </span><br><span class="line"> <span class="comment">//int arr[10]={1,2,3,4,5,6,7,8,9,10}</span></span><br><span class="line"> <span class="comment">//int i = 0;</span></span><br><span class="line"> <span class="comment">//int* p = arr;</span></span><br><span class="line"> <span class="comment">//for (i = 0;i < 10;i++)</span></span><br><span class="line"> <span class="comment">//{</span></span><br><span class="line"> <span class="comment">// //printf("%d ", *(p + i));</span></span><br><span class="line"> <span class="comment">// //printf("%d ",p[i]);</span></span><br><span class="line"> <span class="comment">// //printf("%d ", *(arr + i));</span></span><br><span class="line"> <span class="comment">// printf("%d ", arr[i]);//arr[i] == *(a+i) == *(p+i) == p[i]</span></span><br><span class="line"> <span class="comment">//}</span></span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p>练习:</p>
<figure class="highlight c"><table><tr><td class="code"><pre><span class="line"><span class="type">int</span> arr[<span class="number">5</span>];<span class="comment">//arr是一个含5个元素的整形数组</span></span><br><span class="line"><span class="type">int</span> *parr1[<span class="number">10</span>];<span class="comment">//parr1是一个数组,数组有10个元素,每个元素的类型是int*,故parr1是指针数组</span></span><br><span class="line"><span class="type">int</span> (*parr2)[<span class="number">10</span>];<span class="comment">//parr2是数组指针,它指向一个含10个元素的数组,数组的元素类型是int</span></span><br><span class="line"><span class="type">int</span> (*parr3[<span class="number">10</span>])[<span class="number">5</span>];<span class="comment">//parr3是数组,该数组的有10个元素,每个元素是一个数组指针;该数组指针指向的数组有5个元素,每个元素类型是int</span></span><br></pre></td></tr></table></figure>
<h3 id="数组参数、指针参数">数组参数、指针参数</h3>
<h4 id="一维数组传参">一维数组传参</h4>
<figure class="highlight c"><table><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"><span class="type">void</span> <span class="title function_">test</span><span class="params">(<span class="type">int</span> arr[])</span><span class="comment">//ok</span></span><br><span class="line">{}</span><br><span class="line"><span class="type">void</span> <span class="title function_">test</span><span class="params">(<span class="type">int</span> arr[<span class="number">10</span>])</span><span class="comment">//ok</span></span><br><span class="line">{}</span><br><span class="line"><span class="type">void</span> <span class="title function_">test</span><span class="params">(<span class="type">int</span> *arr)</span><span class="comment">//ok</span></span><br><span class="line">{}</span><br><span class="line"><span class="type">void</span> <span class="title function_">test2</span><span class="params">(<span class="type">int</span> *arr[<span class="number">20</span>])</span><span class="comment">//ok</span></span><br><span class="line">{}</span><br><span class="line"><span class="type">void</span> <span class="title function_">test2</span><span class="params">(<span class="type">int</span> **arr)</span><span class="comment">//ok//一级指针的地址存放在二级指针里 int **arr</span></span><br><span class="line">{}</span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="type">int</span> arr[<span class="number">10</span>] = {<span class="number">0</span>};</span><br><span class="line"> <span class="type">int</span> *arr2[<span class="number">20</span>] = {<span class="number">0</span>};</span><br><span class="line"> test(arr);</span><br><span class="line"> test2(arr2);</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<h4 id="二维数组传参">二维数组传参</h4>
<p><strong>二维数组的数组名是首元素的地址,传参时,传入的是<font color=red>数组第一行地址</font></strong></p>
<figure class="highlight c"><table><tr><td class="code"><pre><span class="line"><span class="type">void</span> <span class="title function_">test</span><span class="params">(<span class="type">int</span> arr[<span class="number">3</span>][<span class="number">5</span>])</span></span><br><span class="line">{}</span><br><span class="line"><span class="type">void</span> <span class="title function_">test1</span><span class="params">(<span class="type">int</span> arr[][<span class="number">5</span>])</span><span class="comment">//数组传参,行可以省略,列不可以省略</span></span><br><span class="line">{}</span><br><span class="line"><span class="type">void</span> <span class="title function_">test2</span><span class="params">(<span class="type">int</span> arr[][])</span><span class="comment">//error</span></span><br><span class="line">{}</span><br><span class="line"><span class="type">void</span> <span class="title function_">test3</span><span class="params">(<span class="type">int</span> *arr)</span><span class="comment">//error,二维数组无法存放整形指针中去</span></span><br><span class="line">{}</span><br><span class="line"><span class="type">void</span> <span class="title function_">test4</span><span class="params">(<span class="type">int</span>** arr)</span><span class="comment">//error,数组名是第一行地址,而二级指针存放是一级变量(指针)的地址</span></span><br><span class="line">{}</span><br><span class="line"><span class="comment">//总结:二维数组传参,函数形参的设计只能省略第一个[]的数字。</span></span><br><span class="line"><span class="comment">//因为对一个二维数组,可以不知道有多少行,但是必须知道一行多少元素。</span></span><br><span class="line"><span class="comment">//这样才方便运算。</span></span><br><span class="line"><span class="type">void</span> <span class="title function_">test5</span><span class="params">(<span class="type">int</span>* arr[<span class="number">5</span>])</span><span class="comment">//ok?//error</span></span><br><span class="line">{}</span><br><span class="line"><span class="type">void</span> <span class="title function_">test6</span><span class="params">(<span class="type">int</span> (*arr)[<span class="number">5</span>])</span><span class="comment">//ok?//ok</span></span><br><span class="line">{}</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="type">int</span> arr[<span class="number">3</span>][<span class="number">5</span>] = {<span class="number">0</span>};</span><br><span class="line"> test(arr);</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<h4 id="一级指针传参">一级指针传参</h4>
<figure class="highlight c"><table><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"><span class="type">void</span> <span class="title function_">print</span><span class="params">(<span class="type">int</span> *p, <span class="type">int</span> sz)</span></span><br><span class="line">{</span><br><span class="line"> <span class="type">int</span> i = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">for</span>(i=<span class="number">0</span>; i<sz; i++)</span><br><span class="line"> {</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d\n"</span>, *(p+i));</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="type">int</span> arr[<span class="number">10</span>] = {<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span>,<span class="number">7</span>,<span class="number">8</span>,<span class="number">9</span>};</span><br><span class="line"> <span class="type">int</span> *p = arr;</span><br><span class="line"> <span class="type">int</span> sz = <span class="keyword">sizeof</span>(arr)/<span class="keyword">sizeof</span>(arr[<span class="number">0</span>]);</span><br><span class="line"> <span class="comment">//一级指针p,传给函数</span></span><br><span class="line"> print(p, sz);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p><strong>思考:</strong></p>
<blockquote>
<p>当一个函数的参数部分为一级指针的时候,函数能接收什么参数?</p>
<figure class="highlight c"><table><tr><td class="code"><pre><span class="line"><span class="number">1.</span><span class="type">void</span> <span class="title function_">test1</span><span class="params">(<span class="type">int</span> *p)</span></span><br><span class="line"> {}</span><br><span class="line"><span class="comment">//test1函数能接收什么参数? test1(&a);或test1(p1);</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{ <span class="type">int</span> a=<span class="number">10</span>;</span><br><span class="line"> <span class="type">int</span>* p1=&a;</span><br><span class="line"> test1(&a);</span><br><span class="line"> test1(p1);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="number">2.</span> <span class="type">void</span> <span class="title function_">test2</span><span class="params">(<span class="type">char</span>* p)</span></span><br><span class="line"> {}</span><br><span class="line"><span class="comment">//test2函数能接收什么参数? test2(&ch);或test2(str);</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{ <span class="type">char</span> ch=<span class="string">'w'</span>;</span><br><span class="line"> <span class="type">char</span>* str=&ch;</span><br><span class="line"> test2(&ch);</span><br><span class="line"> test2(str);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
</blockquote>
<h4 id="二级指针传参">二级指针传参</h4>
<figure class="highlight c"><table><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"><span class="type">void</span> <span class="title function_">test</span><span class="params">(<span class="type">int</span>** ptr)</span></span><br><span class="line">{</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"num = %d\n"</span>, **ptr); </span><br><span class="line">}</span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="type">int</span> n = <span class="number">10</span>;</span><br><span class="line"> <span class="type">int</span> *p = &n;</span><br><span class="line"> <span class="type">int</span> **pp = &p;<span class="comment">//取一级指针p的地址</span></span><br><span class="line"> test(pp);</span><br><span class="line"> test(&p);</span><br><span class="line"> </span><br><span class="line"> <span class="type">int</span>* arr[<span class="number">10</span>];<span class="comment">//指针数组,数组里每个元素都是一级指针</span></span><br><span class="line"> test(arr);<span class="comment">//传过去的是arr数组首元素的地址</span></span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p>思考:</p>
<blockquote>
<p>当函数的参数为二级指针的时候,可以接收什么参数?</p>
<figure class="highlight c"><table><tr><td class="code"><pre><span class="line">><span class="type">void</span> <span class="title function_">test</span><span class="params">(<span class="type">char</span> **p)</span></span><br><span class="line">>{</span><br><span class="line"> </span><br><span class="line">>}</span><br><span class="line">><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">>{</span><br><span class="line"> <span class="type">char</span> c = <span class="string">'b'</span>;</span><br><span class="line"> <span class="type">char</span>*pc = &c;</span><br><span class="line"> <span class="type">char</span>**ppc = &pc;</span><br><span class="line"> <span class="type">char</span>* arr[<span class="number">10</span>];<span class="comment">//指针数组,数组里每个元素都是一级指针</span></span><br><span class="line"> test(&pc);</span><br><span class="line"> test(ppc);</span><br><span class="line"> test(arr);<span class="comment">//Ok?//ok//传过去的是arr数组首元素的地址</span></span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">>}</span><br></pre></td></tr></table></figure>
</blockquote>
<h2 id="函数指针">函数指针</h2>
<p><strong>&函数名 和 函数名 都是函数的地址</strong></p>
<p>函数指针定义示例:</p>
<pre><code>int Add(int x, int y)
{}
int (*pa)(int ,int ) = Add;
printf("%d\n", (*pa)(2, 3));
</code></pre>
<figure class="highlight c"><table><tr><td class="code"><pre><span class="line"><span class="comment">//函数指针 - 是指向函数的指针 -存放函数地址的指针</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">Add</span><span class="params">(<span class="type">int</span> x, <span class="type">int</span> y)</span></span><br><span class="line">{</span><br><span class="line"> <span class="type">int</span> z = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">return</span> z = x + y;</span><br><span class="line">}</span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="type">int</span> a = <span class="number">10</span>;</span><br><span class="line"> <span class="type">int</span> b = <span class="number">20</span>;</span><br><span class="line"> <span class="comment">/*printf("%d ", Add(a, b));*/</span></span><br><span class="line"> <span class="comment">//&函数名 和 函数名 都是函数的地址</span></span><br><span class="line"> <span class="comment">//printf("%p\n", &Add);//打印出函数的地址</span></span><br><span class="line"> <span class="comment">//printf("%p\n", Add);//打印出函数的地址</span></span><br><span class="line"></span><br><span class="line"> <span class="type">int</span> (*pa)(<span class="type">int</span> ,<span class="type">int</span> ) = Add;</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d\n"</span>, (*pa)(<span class="number">2</span>, <span class="number">3</span>));<span class="comment">//5</span></span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p><strong>用法:</strong></p>
<figure class="highlight c"><table><tr><td class="code"><pre><span class="line"><span class="type">void</span> <span class="title function_">Print</span><span class="params">(<span class="type">char</span>* str)</span></span><br><span class="line">{</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%s\n"</span>, str);</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="type">void</span> (*p)(<span class="type">char</span>*) = Print;</span><br><span class="line"> (*p)(<span class="string">"hello bit"</span>);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p><strong>课件:</strong></p>
<figure class="highlight c"><table><tr><td class="code"><pre><span class="line"><span class="type">void</span> <span class="title function_">test</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"hehe\n"</span>);</span><br><span class="line">}</span><br><span class="line"><span class="comment">//下面pfun1和pfun2哪个有能力存放test函数的地址?</span></span><br><span class="line"><span class="type">void</span> (*pfun1)();</span><br><span class="line"><span class="comment">//pfun1可以存放。pfun1先和*结合,说明pfun1是指针,指针指向的是一个函数,指向的函数无参数,返回值类型为void。</span></span><br><span class="line"><span class="type">void</span> *<span class="title function_">pfun2</span><span class="params">()</span>;</span><br><span class="line"><span class="comment">//pfun2表明这是一个函数,其返回类型是void*</span></span><br></pre></td></tr></table></figure>
</article><div class="post-tools" id="post-tools"><div class="post-tools-left"><div class="rewardLeftButton"><div class="post-reward" onclick="anzhiyu.addRewardMask()"><div class="reward-button button--animated" title="赞赏作者"><i class="anzhiyufont anzhiyu-icon-hand-heart-fill"></i>打赏作者</div><div class="reward-main"><div class="reward-all"><span class="reward-title">感谢你赐予我前进的力量</span><ul class="reward-group"><li class="reward-item"><a href="https://cdn.staticaly.com/gh/AnZhiJJ/Blog_Img@main/博客站点配图/wx.2a62ljcc2n40.png" rel="external nofollow noreferrer" target="_blank"><img class="post-qr-code-img" src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-lazy-src="https://cdn.staticaly.com/gh/AnZhiJJ/Blog_Img@main/博客站点配图/wx.2a62ljcc2n40.png" alt="wechat"/></a><div class="post-qr-code-desc">wechat</div></li><li class="reward-item"><a href="https://cdn.staticaly.com/gh/AnZhiJJ/Blog_Img@main/博客站点配图/zfb.23605lp4hi1s.png" rel="external nofollow noreferrer" target="_blank"><img class="post-qr-code-img" src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-lazy-src="https://cdn.staticaly.com/gh/AnZhiJJ/Blog_Img@main/博客站点配图/zfb.23605lp4hi1s.png" alt="alipay"/></a><div class="post-qr-code-desc">alipay</div></li></ul><a class="reward-main-btn" href="/about/#about-reward" target="_blank"><div class="reward-text">赞赏者名单</div><div class="reward-dec">因为你们的支持让我意识到写文章的价值🙏</div></a></div></div><div id="quit-box" onclick="anzhiyu.removeRewardMask()" style="display: none"></div></div></div><div class="shareRight"><div class="share-link mobile"><div class="share-qrcode"><div class="share-button" title="使用手机访问这篇文章"><i class="anzhiyufont anzhiyu-icon-qrcode"></i></div><div class="share-main"><div class="share-main-all"><div id="qrcode" title="https://www.beidaoaz.top/C9.html"></div><div class="reward-dec">使用手机访问这篇文章</div></div></div></div></div><div class="share-link weibo"><a class="share-button" target="_blank" href="https://service.weibo.com/share/share.php?title=C进阶语法(九)指针的进阶&url=https://www.beidaoaz.top/C9.html&pic=https://cdn.statically.io/gh/AnZhiJJ/Blog_Img@main/博客站点配图/image.3qlx7labk120.png" rel="external nofollow noreferrer noopener"><i class="anzhiyufont anzhiyu-icon-weibo"></i></a></div><div class="share-link copyurl"><div class="share-button" id="post-share-url" title="复制链接" onclick="rm.copyPageUrl()"><i class="anzhiyufont anzhiyu-icon-link"></i></div></div></div></div><div class="post-tools-right"><div class="tag_share"><div class="post-meta__box"><div class="post-meta__box__tag-list"><a class="post-meta__box__tags" href="tags/C%E8%AF%AD%E6%B3%95/"><span class="tags-punctuation"><i class="anzhiyufont anzhiyu-icon-tag"></i></span>C语法<span class="tagsPageCount">2</span></a><a class="post-meta__box__tags" href="tags/%E6%8C%87%E9%92%88/"><span class="tags-punctuation"><i class="anzhiyufont anzhiyu-icon-tag"></i></span>指针<span class="tagsPageCount">1</span></a></div></div></div><div class="post_share"><div class="social-share" data-image="https://cdn.statically.io/gh/AnZhiJJ/Blog_Img@Science+/read_paper/1.kupf912yxi8.webp" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://npm.elemecdn.com/[email protected]/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"/><script src="https://npm.elemecdn.com/[email protected]/sharejs/dist/js/social-share.min.js" defer="defer"></script></div></div></div><div class="post-copyright"><i class="anzhiyufont anzhiyu-icon-copyright"></i><div class="post-copyright__author"><a class="post-copyright__original" title="该文章为原创文章,注意版权协议" href="https://www.beidaoaz.top/C9.html">原创</a><a class="post-copyright-title"><span>C进阶语法(九)指针的进阶</span></a></div><div class="post-copyright-info-box"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"></span><a class="link" href="https://www.beidaoaz.top">Beidaos</a></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a class="link" href="https://www.beidaoaz.top/C9.html">https://www.beidaoaz.top/C9.html</a></span><span class="copy-button" onclick="rm.copyPageUrl()"><i class="anzhiyufont anzhiyu-icon-copy"></i></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外,均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" rel="external nofollow noreferrer" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://www.beidaoaz.top" target="_blank">BEIDAO.</a>!</span></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="C8.html"><img class="prev-cover" src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-lazy-src="https://cdn.statically.io/gh/AnZhiJJ/Blog_Img@main/博客站点配图/image.3qlx7labk120.png" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">C进阶语法(八)数据的存储</div></div></a></div><div class="next-post pull-right"><a href="techdoc1.html"><img class="next-cover" src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-lazy-src="https://cdn.statically.io/gh/AnZhiJJ/Blog_Img@main/博客站点配图/image.3qlx7labk120.png" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">Conda常用操作及源的配置、pip源的配置</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="anzhiyufont anzhiyu-icon-thumbs-up fa-fw" style="font-size: 1.5rem; margin-right: 4px"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="C8.html" title="C进阶语法(八)数据的存储"><img class="cover" src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-lazy-src="https://cdn.statically.io/gh/AnZhiJJ/Blog_Img@main/博客站点配图/image.3qlx7labk120.png" alt="cover"><div class="content is-center"><div class="date"><i class="anzhiyufont anzhiyu-icon-calendar-days fa-fw"></i> 2023-04-08</div><div class="title">C进阶语法(八)数据的存储</div></div></a></div></div></div><hr/><div id="post-comment"><div class="comment-head"><div class="comment-headline"><i class="anzhiyufont anzhiyu-icon-comments"></i><span> 评论</span></div><div class="comment-randomInfo" style="display: none"><a onclick="anzhiyu.addRandomCommentInfo()" href="javascript:void(0)" rel="external nofollow noreferrer">匿名评论</a></div><div class="comment-tips" id="comment-tips"><span>你无需删除空行,直接评论以获取最佳展示效果</span></div></div><div class="comment-wrap"><div><div id="twikoo-wrap"></div></div></div><div class="comment-barrage"></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="author-info-top"> <div class="card-info-avatar"><a class="avatar-img" href="/about"><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-lazy-src="https://cdn.staticaly.com/gh/AnZhiJJ/Blog_Img@main/博客站点配图/avatar.1tq7dqdicpmo.png" onerror="this.onerror=null;this.src='../private_ismg/404.gif'" alt="avatar"/></a></div></div><div class="author-info__sayhi" id="author-info__sayhi"></div><h1 class="author-info__name">Beidaos</h1><div class="author-info__description">Live yourself as a ray of light.</div><div class="card-info-social-icons is-center"><a class="social-icon faa-parent animated-hover" href="https://github.com/AnZhiJJ" rel="external nofollow noreferrer" target="_blank" title="Github"><i class="anzhiyufont anzhiyu-icon-github"></i></a><a class="social-icon faa-parent animated-hover" href="https://mail.qq.com/cgi-bin/qm_share?t=qm_mailme&[email protected]" rel="external nofollow noreferrer" target="_blank" title="Email"><i class="anzhiyufont anzhiyu-icon-envelope"></i></a><a class="social-icon faa-parent animated-hover" href="https://www.ithome.com/" rel="external nofollow noreferrer" target="_blank" title="ItHome"><i class="anzhiyufont anzhiyu-icon-rss"></i></a></div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="anzhiyufont anzhiyu-icon-bars"></i><span>文章目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%8C%87%E9%92%88%E5%9F%BA%E7%A1%80"><span class="toc-text">指针基础</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%AD%97%E7%AC%A6%E6%8C%87%E9%92%88"><span class="toc-text">字符指针</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%8C%87%E9%92%88%E6%95%B0%E7%BB%84"><span class="toc-text">指针数组</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%95%B0%E7%BB%84%E6%8C%87%E9%92%88"><span class="toc-text">数组指针</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%95%B0%E7%BB%84%E6%8C%87%E9%92%88%E7%9A%84%E5%AE%9A%E4%B9%89"><span class="toc-text">数组指针的定义</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%95%B0%E7%BB%84%E5%90%8DVS%E6%95%B0%E7%BB%84%E5%90%8D"><span class="toc-text">&数组名VS数组名</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%95%B0%E7%BB%84%E6%8C%87%E9%92%88%E7%94%A8%E6%B3%95"><span class="toc-text">数组指针用法</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%94%A8%E6%B3%951%EF%BC%9A"><span class="toc-text">用法1:</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%94%A8%E6%B3%952-%E5%9C%B0%E5%9D%80%E4%BC%A0%E5%8F%82"><span class="toc-text">用法2:地址传参</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%95%B0%E7%BB%84%E5%8F%82%E6%95%B0%E3%80%81%E6%8C%87%E9%92%88%E5%8F%82%E6%95%B0"><span class="toc-text">数组参数、指针参数</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%B8%80%E7%BB%B4%E6%95%B0%E7%BB%84%E4%BC%A0%E5%8F%82"><span class="toc-text">一维数组传参</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%BA%8C%E7%BB%B4%E6%95%B0%E7%BB%84%E4%BC%A0%E5%8F%82"><span class="toc-text">二维数组传参</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%B8%80%E7%BA%A7%E6%8C%87%E9%92%88%E4%BC%A0%E5%8F%82"><span class="toc-text">一级指针传参</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%BA%8C%E7%BA%A7%E6%8C%87%E9%92%88%E4%BC%A0%E5%8F%82"><span class="toc-text">二级指针传参</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%87%BD%E6%95%B0%E6%8C%87%E9%92%88"><span class="toc-text">函数指针</span></a></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="anzhiyufont anzhiyu-icon-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="" title="卷积神经网络-CNN"><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-lazy-src="https://cdn.statically.io/gh/AnZhiJJ/Blog_Img@Science+/read_paper/1.kupf912yxi8.webp" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="卷积神经网络-CNN"/></a><div class="content"><a class="title" href="" title="卷积神经网络-CNN">卷积神经网络-CNN</a><time datetime="2023-07-26T00:00:00.000Z" title="发表于 2023-07-26 08:00:00">2023-07-26</time></div></div><div class="aside-list-item"><a class="thumbnail" href="deepLearning02.html" title="神经网络中的反向传播算法——BackPropagation算法"><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-lazy-src="https://cdn.statically.io/gh/AnZhiJJ/Blog_Img@Science+/read_paper/1.kupf912yxi8.webp" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="神经网络中的反向传播算法——BackPropagation算法"/></a><div class="content"><a class="title" href="deepLearning02.html" title="神经网络中的反向传播算法——BackPropagation算法">神经网络中的反向传播算法——BackPropagation算法</a><time datetime="2023-07-25T10:00:00.000Z" title="发表于 2023-07-25 18:00:00">2023-07-25</time></div></div><div class="aside-list-item"><a class="thumbnail" href="deepLearning01.html" title="神经网络基础"><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-lazy-src="https://cdn.statically.io/gh/AnZhiJJ/Blog_Img@Science+/read_paper/1.kupf912yxi8.webp" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="神经网络基础"/></a><div class="content"><a class="title" href="deepLearning01.html" title="神经网络基础">神经网络基础</a><time datetime="2023-07-25T00:00:00.000Z" title="发表于 2023-07-25 08:00:00">2023-07-25</time></div></div><div class="aside-list-item"><a class="thumbnail" href="latex1.html" title="【LaTeX】新手教程:从入门到日常使用"><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-lazy-src="https://cdn.statically.io/gh/AnZhiJJ/Blog_Img@main/博客站点配图/image.3qlx7labk120.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="【LaTeX】新手教程:从入门到日常使用"/></a><div class="content"><a class="title" href="latex1.html" title="【LaTeX】新手教程:从入门到日常使用">【LaTeX】新手教程:从入门到日常使用</a><time datetime="2023-07-23T03:00:00.000Z" title="发表于 2023-07-23 11:00:00">2023-07-23</time></div></div><div class="aside-list-item"><a class="thumbnail" href="pythonadv_OOP_6.html" title="Python进阶(六)案例:面向对象版学员管理系统"><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-lazy-src="https://cdn.statically.io/gh/AnZhiJJ/Blog_Img@main/博客站点配图/python-logo.6puefdh4wtk0.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Python进阶(六)案例:面向对象版学员管理系统"/></a><div class="content"><a class="title" href="pythonadv_OOP_6.html" title="Python进阶(六)案例:面向对象版学员管理系统">Python进阶(六)案例:面向对象版学员管理系统</a><time datetime="2023-07-09T00:00:00.000Z" title="发表于 2023-07-09 08:00:00">2023-07-09</time></div></div><div class="aside-list-item"><a class="thumbnail" href="pythonadv_OOP_5.html" title="Python进阶(五)模块和包"><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-lazy-src="https://cdn.statically.io/gh/AnZhiJJ/Blog_Img@main/博客站点配图/python-logo.6puefdh4wtk0.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Python进阶(五)模块和包"/></a><div class="content"><a class="title" href="pythonadv_OOP_5.html" title="Python进阶(五)模块和包">Python进阶(五)模块和包</a><time datetime="2023-07-09T00:00:00.000Z" title="发表于 2023-07-09 08:00:00">2023-07-09</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div id="footer_deal"><a class="deal_link" href="../mailto:[email protected]" title="email"><i class="anzhiyufont anzhiyu-icon-envelope"></i></a><a class="deal_link" target="_blank" rel="noopener external nofollow noreferrer" href="https://weibo.com/u/2792503557" title="微博"><i class="anzhiyufont anzhiyu-icon-weibo"></i></a><img class="footer_mini_logo" title="返回顶部" onclick="anzhiyu.scrollToDest(0, 500)" src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-lazy-src="https://cdn.staticaly.com/gh/AnZhiJJ/Blog_Img@main/博客站点配图/avatar.1tq7dqdicpmo.png"/><a class="deal_link" target="_blank" rel="noopener external nofollow noreferrer" href="https://github.com/AnZhiJJ" title="Github"><i class="anzhiyufont anzhiyu-icon-github"></i></a><a class="deal_link" target="_blank" rel="noopener external nofollow noreferrer" href="https://space.bilibili.com/360398463" title="Bilibili"><i class="anzhiyufont anzhiyu-icon-bilibili"></i></a></div></div><div id="ah-footer"><div class="footer-group"><h3 class="footer-title">博客文章</h3><div class="footer-links"><a class="footer-item" href="/archives/" data-pjax-state="">全部文章</a><a class="footer-item" href="/tags/" data-pjax-state="">文章标签</a><a class="footer-item" href="/categories/" data-pjax-state="">文章分类</a></div></div><div class="footer-group"><h3 class="footer-title">学习笔记</h3><div class="footer-links"><a class="footer-item" href="/categories/C语法/" data-pjax-state="">C 语法</a><a class="footer-item" href="/categories/Python语法/" data-pjax-state="">Python语法</a><a class="footer-item" href="/categories/技术文档/" data-pjax-state="">技术文档</a></div></div><div class="footer-group"><h3 class="footer-title">博客链接</h3><div class="footer-links"><a class="footer-item" href="/album/" data-pjax-state="">相册集</a><a class="footer-item" href="/collect/" data-pjax-state="">藏宝阁</a><a class="footer-item" href="/essay/" data-pjax-state="">闲言碎语</a></div></div><div class="footer-group"><h3 class="footer-title">关于本站</h3><div class="footer-links"><a class="footer-item" href="/about/" data-pjax-state="">关于博主</a><a class="footer-item" href="/cc/" data-pjax-state="">版权协议</a></div></div></div><div id="fotter-copyright" style="font-size:100%"><div class="copyrights"><span>Copyright © 2022 Beidaos. </span><span> All rights reserved.</span></div><div class="framework-info"><span>Powered by </span><a target="_blank" rel="noopener external nofollow noreferrer" href="https://hexo.io">Hexo</a><span class="footer-separator">&</span><a target="_blank" rel="noopener external nofollow noreferrer" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-lazy-src="https://cdn.staticaly.com/gh/AnZhiJJ/Blog_Img@main/博客站点配图/avatar.1tq7dqdicpmo.png" onerror="onerror=null;src='/private_ismg/404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="archives/" title="archive"><div class="headline">文章</div><div class="length-num">32</div></a><a href="tags/" title="tag"><div class="headline">标签</div><div class="length-num">16</div></a><a href="categories/" title="category"><div class="headline">分类</div><div class="length-num">8</div></a></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page faa-parent animated-hover" href="javascript:void(0);" rel="external nofollow noreferrer"><span> 文章</span></a><ul class="menus_item_child" style="left:-79px;"><li><a class="site-page child faa-parent animated-hover" href="/archives/"><i class="anzhiyufont anzhiyu-icon-box-archive faa-tada" style="font-size: 0.9em;"></i><span> 归档</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/categories/"><i class="anzhiyufont anzhiyu-icon-shapes faa-tada" style="font-size: 0.9em;"></i><span> 分类</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/tags/"><svg class="icon faa-tada" aria-hidden="true"><use xlink:href="#icon-biaoqiantags3"></use></svg><span> 标签</span></a></li></ul></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="javascript:void(0);" rel="external nofollow noreferrer"><span> 社交</span></a><ul class="menus_item_child" style="left:-31px;"><li><a class="site-page child faa-parent animated-hover" href="/link/"><i class="anzhiyufont anzhiyu-icon-link faa-tada" style="font-size: 0.9em;"></i><span> 友链</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/essay/"><i class="anzhiyufont anzhiyu-icon-lightbulb faa-tada" style="font-size: 0.9em;"></i><span> 闲言碎语</span></a></li></ul></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="javascript:void(0);" rel="external nofollow noreferrer"><span> 我的</span></a><ul class="menus_item_child" style="left:-79px;"><li><a class="site-page child faa-parent animated-hover" href="/music/?id=886726002&server=netease"><i class="anzhiyufont anzhiyu-icon-music faa-tada" style="font-size: 0.9em;"></i><span> 音乐馆</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/collect/"><svg class="icon faa-tada" aria-hidden="true"><use xlink:href="#icon-dropboxdropbox4"></use></svg><span> 藏宝阁</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/album/"><i class="anzhiyufont anzhiyu-icon-images faa-tada" style="font-size: 0.9em;"></i><span> 相册集</span></a></li></ul></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="javascript:void(0);" rel="external nofollow noreferrer"><span> 关于</span></a><ul class="menus_item_child" style="left:17px;"><li><a class="site-page child faa-parent animated-hover" href="/about/"><i class="anzhiyufont anzhiyu-icon-paper-plane faa-tada" style="font-size: 0.9em;"></i><span> 关于博主</span></a></li></ul></div></div></div></div><div id="keyboard-tips"><div class="keyboardTitle">博客快捷键</div><div class="keybordList"><div class="keybordItem"><div class="keyGroup"><div class="key">shift K</div></div><div class="keyContent"><div class="content">关闭快捷键功能</div></div></div><div class="keybordItem"><div class="keyGroup"><div class="key">shift A</div></div><div class="keyContent"><div class="content">打开中控台</div></div></div><div class="keybordItem"><div class="keyGroup"><div class="key">shift M</div></div><div class="keyContent"><div class="content">播放/暂停音乐</div></div></div><div class="keybordItem"><div class="keyGroup"><div class="key">shift D</div></div><div class="keyContent"><div class="content">深色/浅色显示模式</div></div></div><div class="keybordItem"><div class="keyGroup"><div class="key">shift S</div></div><div class="keyContent"><div class="content">站内搜索</div></div></div><div class="keybordItem"><div class="keyGroup"><div class="key">shift R</div></div><div class="keyContent"><div class="content">随机访问</div></div></div><div class="keybordItem"><div class="keyGroup"><div class="key">shift H</div></div><div class="keyContent"><div class="content">返回首页</div></div></div><div class="keybordItem"><div class="keyGroup"><div class="key">shift F</div></div><div class="keyContent"><div class="content">友链鱼塘</div></div></div><div class="keybordItem"><div class="keyGroup"><div class="key">shift L</div></div><div class="keyContent"><div class="content">友链页面</div></div></div><div class="keybordItem"><div class="keyGroup"><div class="key">shift P</div></div><div class="keyContent"><div class="content">关于本站</div></div></div><div class="keybordItem"><div class="keyGroup"><div class="key">shift I</div></div><div class="keyContent"><div class="content">原版右键菜单</div></div></div></div></div><script>var anzhiyu_keyboard = false
var anzhiyu_intype = false
var anzhiyu_keyUpEvent_timeoutId = null
function addKeyShotListener() {
const windowObject = window;
windowObject.removeEventListener("keydown", keyDownEvent);
windowObject.removeEventListener("keyup", keyUpEvent);
windowObject.addEventListener("keydown", keyDownEvent);
windowObject.addEventListener("keyup", keyUpEvent);
}
function keyDownEvent(event) {
const isEscapeKeyPressed = event.keyCode === 27;
const isShiftKeyPressed = event.shiftKey;
const isKeyboardEnabled = anzhiyu_keyboard;
const isInInputField = anzhiyu_intype;
if (isEscapeKeyPressed) {
anzhiyu.hideLoading();
anzhiyu.hideConsole();
rm.hideRightMenu();
}
if (isKeyboardEnabled && isShiftKeyPressed && !isInInputField) {
switch (event.keyCode) {
case 16:
anzhiyu_keyUpEvent_timeoutId = setTimeout(()=>{
document.querySelector("#keyboard-tips").classList.add("show");
}, 200);
break;
case 65:
anzhiyu.showConsole();
break;
case 77:
anzhiyu.musicToggle();
case 75:
anzhiyu.keyboardToggle();
case 73:
anzhiyu.rightMenuToggle();
break;
case 82:
toRandomPost();
break;
case 72:
pjax.loadUrl("/");
break;
case 68:
rm.switchDarkMode();
break;
case 70:
pjax.loadUrl("/fcircle/");
break;
case 76:
pjax.loadUrl("/link/");
break;
case 80:
pjax.loadUrl("/about/");
break;
default:
break;
}
event.preventDefault();
}
}
addKeyShotListener();
window.onfocus = function() {
document.getElementById("keyboard-tips").classList.remove("show")
}
function setInputFocusListener() {
const inputs = document.querySelectorAll("input, textarea");
inputs.forEach((input) => {
input.addEventListener("focus", () => {
setAnzhiyuIntype(true);
});
input.addEventListener("blur", () => {
setAnzhiyuIntype(false);
});
});
}
function setanzhiyuIntype(value) {
anzhiyu_intype = value;
}
function keyUpEvent(event) {
anzhiyu_keyUpEvent_timeoutId && clearTimeout(anzhiyu_keyUpEvent_timeoutId);
if (event.keyCode === 16) {
const keyboardTips = document.querySelector("#keyboard-tips");
keyboardTips.classList.remove("show");
}
}
function listenToPageInputPress() {
const inputElement = document.getElementById("toPageText");
const buttonElement = document.getElementById("toPageButton");
if (!inputElement) {
return;
}
inputElement.addEventListener("keydown", (event) => {
if (event.keyCode === 13) {
event.preventDefault();
anzhiyu.toPage();
pjax.loadUrl(buttonElement.href);
}
});
inputElement.addEventListener("input", () => {
const value = inputElement.value;
if (!value || value === "0") {
buttonElement.classList.remove("haveValue");
} else {
buttonElement.classList.add("haveValue");
}
const pageNumbers = document.querySelectorAll(".page-number");
const maxPageNumber = +pageNumbers[pageNumbers.length - 1].innerHTML;
if (+value > maxPageNumber) {
inputElement.value = maxPageNumber;
}
});
}
setInputFocusListener()</script><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="anzhiyufont anzhiyu-icon-book-open"></i></button><button id="translateLink" type="button" title="简繁转换">繁</button><button type="button" title="切换背景" onclick="toggleWinbox()"><i class="iconfont icon-qiehuankapian"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="anzhiyufont anzhiyu-icon-circle-half-stroke"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="anzhiyufont anzhiyu-icon-arrows-left-right"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="anzhiyufont anzhiyu-icon-gear"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="anzhiyufont anzhiyu-icon-list-ul"></i></button><a id="to_comment" href="#post-comment" title="直达评论"><i class="anzhiyufont anzhiyu-icon-comments"></i></a><a id="switch_commentBarrage" href="javascript:anzhiyu.switchCommentBarrage();" rel="external nofollow noreferrer" title="开关弹幕"><i class="anzhiyufont anzhiyu-icon-danmu"></i></a><button id="center-console" type="button" title="中控台"><i class="iconfont icon-kongzhitai"></i></button><button id="go-up" type="button" title="回到顶部"><i class="anzhiyufont anzhiyu-icon-arrow-up"></i></button></div></div><div id="nav-music"><div id="nav-music-hoverTips" onclick="anzhiyu.musicToggle()">播放音乐</div><div id="console-music-bg"></div><meting-js id="886726002" server="netease" type="playlist" mutex="true" preload="none" theme="var(--anzhiyu-main)" data-lrctype="0" order="random"></meting-js></div><div id="console"><div class="close-btn" onclick="anzhiyu.hideConsole()" href="javascript:void(0);"><i class="anzhiyufont anzhiyu-icon-circle-xmark" style="font-size: 35px;"></i></div><div class="console-card-group-reward"><ul class="reward-all console-card"><li class="reward-item"><a href="https://cdn.staticaly.com/gh/AnZhiJJ/Blog_Img@main/博客站点配图/wx.2a62ljcc2n40.png" rel="external nofollow noreferrer" target="_blank"><img class="post-qr-code-img" alt="wechat" src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-lazy-src="https://cdn.staticaly.com/gh/AnZhiJJ/Blog_Img@main/博客站点配图/wx.2a62ljcc2n40.png"/></a><div class="post-qr-code-desc">wechat</div></li><li class="reward-item"><a href="https://cdn.staticaly.com/gh/AnZhiJJ/Blog_Img@main/博客站点配图/zfb.23605lp4hi1s.png" rel="external nofollow noreferrer" target="_blank"><img class="post-qr-code-img" alt="alipay" src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-lazy-src="https://cdn.staticaly.com/gh/AnZhiJJ/Blog_Img@main/博客站点配图/zfb.23605lp4hi1s.png"/></a><div class="post-qr-code-desc">alipay</div></li></ul></div><div class="console-card-group"><div class="console-card-group-left"><div class="console-card" id="card-newest-comments" onclick="anzhiyu.hideConsole()"><div class="card-content"><div class="author-content-item-tips">互动</div><span class="author-content-item-title"> <span>最新评论</span></span></div><div class="aside-list"><span>正在加载中...</span></div></div></div><div class="console-card-group-right"><div class="console-card tags" onclick="anzhiyu.hideConsole()"><div class="card-content"><div class="author-content-item-tips">兴趣点</div><span class="author-content-item-title">寻找你感兴趣的领域</span><div class="card-tags"><div class="item-headline"></div><div class="card-tag-cloud"><a href="tags/BP%E7%AE%97%E6%B3%95/" style="font-size: 1.05rem;">BP算法<sup>1</sup></a><a href="tags/Catia/" style="font-size: 1.05rem;">Catia<sup>1</sup></a><a href="tags/C%E8%AF%AD%E6%B3%95/" style="font-size: 1.05rem; font-weight: 500; color: var(--anzhiyu-lighttext)">C语法<sup>2</sup></a><a href="tags/DNS/" style="font-size: 1.05rem;">DNS<sup>1</sup></a><a href="tags/LaTeX/" style="font-size: 1.05rem;">LaTeX<sup>1</sup></a><a href="tags/Python%E8%AF%AD%E6%B3%95/" style="font-size: 1.05rem; font-weight: 500; color: var(--anzhiyu-lighttext)">Python语法<sup>16</sup></a><a href="tags/Python%E8%BF%9B%E9%98%B6/" style="font-size: 1.05rem;">Python进阶<sup>6</sup></a><a href="tags/conda/" style="font-size: 1.05rem;">conda<sup>1</sup></a><a href="tags/pip/" style="font-size: 1.05rem;">pip<sup>1</sup></a><a href="tags/%E5%8D%B7%E7%A7%AF%E7%A5%9E%E7%BB%8F%E7%BD%91%E7%BB%9C/" style="font-size: 1.05rem;">卷积神经网络<sup>1</sup></a><a href="tags/%E5%9F%9F%E5%90%8D%E8%A7%A3%E6%9E%90/" style="font-size: 1.05rem;">域名解析<sup>1</sup></a><a href="tags/%E6%8C%87%E9%92%88/" style="font-size: 1.05rem;">指针<sup>1</sup></a><a href="tags/%E6%96%87%E7%8C%AE%E7%AC%94%E8%AE%B0/" style="font-size: 1.05rem;">文献笔记<sup>1</sup></a><a href="tags/%E6%B7%B1%E5%BA%A6%E5%AD%A6%E4%B9%A0/" style="font-size: 1.05rem;">深度学习<sup>3</sup></a><a href="tags/%E7%A5%9E%E7%BB%8F%E7%BD%91%E7%BB%9C/" style="font-size: 1.05rem;">神经网络<sup>1</sup></a><a href="tags/%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1%E7%BC%96%E7%A8%8B/" style="font-size: 1.05rem;">面向对象编程<sup>6</sup></a></div></div><hr/></div></div><div class="console-card history" onclick="anzhiyu.hideConsole()"><div class="item-headline"><i class="anzhiyufont anzhiyu-icon-box-archiv"></i><span>文章</span></div></div></div></div><div class="button-group"><div class="console-btn-item"><a class="darkmode_switchbutton" onclick="rm.switchDarkMode()" title="显示模式切换" href="javascript:void(0);" rel="external nofollow noreferrer"><i class="anzhiyufont anzhiyu-icon-moon"></i></a></div><div class="console-btn-item" id="consoleHideAside" onclick="anzhiyu.hideAsideBtn()" title="边栏显示控制"><a class="asideSwitch"><i class="anzhiyufont anzhiyu-icon-arrows-left-right"></i></a></div><div class="console-btn-item on" id="consoleCommentBarrage" onclick="anzhiyu.switchCommentBarrage()" title="热评开关"><a class="commentBarrage"><i class="anzhiyufont anzhiyu-icon-message"></i></a></div><div class="console-btn-item" id="consoleMusic" onclick="anzhiyu.musicToggle()" title="音乐开关"><a class="music-switch"><i class="anzhiyufont anzhiyu-icon-music"></i></a></div><div class="console-btn-item" id="consoleKeyboard" onclick="anzhiyu.keyboardToggle()" title="快捷键开关"><a class="keyboard-switch"><i class="anzhiyufont anzhiyu-icon-keyboard"></i></a></div></div><div class="console-mask" onclick="anzhiyu.hideConsole()" href="javascript:void(0);" rel="external nofollow noreferrer"></div></div><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="anzhiyufont anzhiyu-icon-xmark"></i></button></nav><div class="is-center" id="loading-database"><i class="anzhiyufont anzhiyu-icon-spinner anzhiyu-pulse-icon"></i><span> 数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><hr/><div id="local-search-results"></div></div></div><div id="search-mask"></div></div><div id="rightMenu"><div class="rightMenu-group rightMenu-small"><div class="rightMenu-item" id="menu-backward"><i class="anzhiyufont anzhiyu-icon-arrow-left"></i></div><div class="rightMenu-item" id="menu-forward"><i class="anzhiyufont anzhiyu-icon-arrow-right"></i></div><div class="rightMenu-item" id="menu-refresh"><i class="anzhiyufont anzhiyu-icon-arrow-rotate-right" style="font-size: 1rem;"></i></div><div class="rightMenu-item" id="menu-top"><i class="anzhiyufont anzhiyu-icon-arrow-up"></i></div></div><div class="rightMenu-group rightMenu-line rightMenuPlugin"><div class="rightMenu-item" id="menu-copytext"><i class="anzhiyufont anzhiyu-icon-copy"></i><span>复制选中文本</span></div><div class="rightMenu-item" id="menu-pastetext"><i class="anzhiyufont anzhiyu-icon-paste"></i><span>粘贴文本</span></div><a class="rightMenu-item" id="menu-commenttext"><i class="anzhiyufont anzhiyu-icon-comment-medical"></i><span>引用到评论</span></a><div class="rightMenu-item" id="menu-newwindow"><i class="anzhiyufont anzhiyu-icon-window-restore"></i><span>新窗口打开</span></div><div class="rightMenu-item" id="menu-copylink"><i class="anzhiyufont anzhiyu-icon-link"></i><span>复制链接地址</span></div><div class="rightMenu-item" id="menu-copyimg"><i class="anzhiyufont anzhiyu-icon-images"></i><span>复制此图片</span></div><div class="rightMenu-item" id="menu-downloadimg"><i class="anzhiyufont anzhiyu-icon-download"></i><span>下载此图片</span></div><div class="rightMenu-item" id="menu-newwindowimg"><i class="anzhiyufont anzhiyu-icon-window-restore"></i><span>新窗口打开图片</span></div><div class="rightMenu-item" id="menu-search"><i class="anzhiyufont anzhiyu-icon-magnifying-glass"></i><span>站内搜索</span></div><div class="rightMenu-item" id="menu-searchBaidu"><i class="anzhiyufont anzhiyu-icon-magnifying-glass"></i><span>百度搜索</span></div><div class="rightMenu-item" id="menu-music-toggle"><i class="anzhiyufont anzhiyu-icon-play"></i><span>播放音乐</span></div><div class="rightMenu-item" id="menu-music-back"><i class="anzhiyufont anzhiyu-icon-backward"></i><span>切换到上一首</span></div><div class="rightMenu-item" id="menu-music-forward"><i class="anzhiyufont anzhiyu-icon-forward"></i><span>切换到下一首</span></div><div class="rightMenu-item" id="menu-music-playlist" onclick="window.open("https://music.163.com/#/my/m/music/playlist?id=886726002", "_blank");" style="display: none;"><i class="anzhiyufont anzhiyu-icon-radio"></i><span>查看所有歌曲</span></div><div class="rightMenu-item" id="menu-music-copyMusicName"><i class="anzhiyufont anzhiyu-icon-copy"></i><span>复制歌名</span></div></div><div class="rightMenu-group rightMenu-line rightMenuOther"><a class="rightMenu-item menu-link" id="menu-randomPost"><i class="anzhiyufont anzhiyu-icon-shuffle"></i><span>随便逛逛</span></a><a class="rightMenu-item menu-link" href="/categories/"><i class="anzhiyufont anzhiyu-icon-cube"></i><span>博客分类</span></a><a class="rightMenu-item menu-link" href="/tags/"><i class="anzhiyufont anzhiyu-icon-tags"></i><span>文章标签</span></a></div><div class="rightMenu-group rightMenu-line rightMenuOther"><a class="rightMenu-item" id="menu-copy" href="javascript:void(0);" rel="external nofollow noreferrer"><i class="anzhiyufont anzhiyu-icon-copy"></i><span>复制地址</span></a><a class="rightMenu-item" id="menu-commentBarrage" href="javascript:void(0);" rel="external nofollow noreferrer"><i class="anzhiyufont anzhiyu-icon-message"></i><span class="menu-commentBarrage-text">关闭热评</span></a><a class="rightMenu-item" id="menu-darkmode" href="javascript:void(0);" rel="external nofollow noreferrer"><i class="anzhiyufont anzhiyu-icon-circle-half-stroke"></i><span class="menu-darkmode-text">深色模式</span></a><a class="rightMenu-item" id="menu-translate" href="javascript:void(0);" rel="external nofollow noreferrer"><i class="anzhiyufont anzhiyu-icon-language"></i><span>轉為繁體</span></a></div></div><div id="rightmenu-mask"></div><div><script src="https://npm.elemecdn.com/[email protected]/source/js/utils.js"></script><script src="https://npm.elemecdn.com/[email protected]/source/js/main.js"></script><script src="https://npm.elemecdn.com/[email protected]/source/js/tw_cn.js"></script><script src="https://npm.elemecdn.com/@fancyapps/[email protected]/dist/fancybox.umd.js"></script><script src="https://npm.elemecdn.com/[email protected]/instantpage.js" type="module"></script><script src="https://npm.elemecdn.com/[email protected]/dist/lazyload.iife.min.js"></script><script src="https://npm.elemecdn.com/[email protected]/dist/snackbar.min.js"></script><canvas id="universe"></canvas><script async src="https://npm.elemecdn.com/[email protected]/dark/dark.js"></script><script>// 消除控制台打印
var HoldLog = console.log;
console.log = function () {};
let now1 = new Date();
queueMicrotask(() => {
const Log = function () {
HoldLog.apply(console, arguments);
}; //在恢复前输出日志
const grt = new Date("04/01/2021 00:00:00"); //此处修改你的建站时间或者网站上线时间
now1.setTime(now1.getTime() + 250);
const days = (now1 - grt) / 1000 / 60 / 60 / 24;
const dnum = Math.floor(days);
const ascll = [
`欢迎使用安知鱼!`,
`生活明朗, 万物可爱`,
`
█████╗ ███╗ ██╗███████╗██╗ ██╗██╗██╗ ██╗██╗ ██╗
██╔══██╗████╗ ██║╚══███╔╝██║ ██║██║╚██╗ ██╔╝██║ ██║
███████║██╔██╗ ██║ ███╔╝ ███████║██║ ╚████╔╝ ██║ ██║
██╔══██║██║╚██╗██║ ███╔╝ ██╔══██║██║ ╚██╔╝ ██║ ██║
██║ ██║██║ ╚████║███████╗██║ ██║██║ ██║ ╚██████╔╝
╚═╝ ╚═╝╚═╝ ╚═══╝╚══════╝╚═╝ ╚═╝╚═╝ ╚═╝ ╚═════╝
`,
"已上线",
dnum,
"天",
"©2022 By 安知鱼",
];
const ascll2 = [`NCC2-036`, `调用前置摄像头拍照成功,识别为【小笨蛋】.`, `Photo captured: `, `🤪`];
setTimeout(
Log.bind(
console,
`\n%c${ascll[0]} %c ${ascll[1]} %c ${ascll[2]} %c${ascll[3]}%c ${ascll[4]}%c ${ascll[5]}\n\n%c ${ascll[6]}\n`,
"color:#3b70fc",
"",
"color:#3b70fc",
"color:#3b70fc",
"",
"color:#3b70fc",
""
)
);
setTimeout(
Log.bind(
console,
`%c ${ascll2[0]} %c ${ascll2[1]} %c \n${ascll2[2]} %c\n${ascll2[3]}\n`,
"color:white; background-color:#4fd953",
"",
"",
'background:url("https://npm.elemecdn.com/[email protected]/img/post/common/tinggge.gif") no-repeat;font-size:450%'
)
);
setTimeout(Log.bind(console, "%c WELCOME %c 你好,小笨蛋.", "color:white; background-color:#4f90d9", ""));
setTimeout(
console.warn.bind(
console,
"%c ⚡ Powered by 安知鱼 %c 你正在访问 Beidaos 的博客.",
"color:white; background-color:#f0ad4e",
""
)
);
setTimeout(Log.bind(console, "%c W23-12 %c 你已打开控制台.", "color:white; background-color:#4f90d9", ""));
setTimeout(
console.warn.bind(console, "%c S013-782 %c 你现在正处于监控中.", "color:white; background-color:#d9534f", "")
);
});</script><script async src="/anzhiyu/random.js"></script><script src="https://npm.elemecdn.com/[email protected]/source/js/search/local-search.js"></script><div class="js-pjax"><script>(()=>{
const init = () => {
twikoo.init(Object.assign({
el: '#twikoo-wrap',
envId: 'https://twikoo.beidaoaz.top/',
region: '',
onCommentLoaded: function () {
anzhiyu.loadLightbox(document.querySelectorAll('#twikoo .tk-content img:not(.tk-owo-emotion)'))
}
}, null))
}
const getCount = () => {
const countELement = document.getElementById('twikoo-count')
if(!countELement) return
twikoo.getCommentsCount({
envId: 'https://twikoo.beidaoaz.top/',
region: '',
urls: [window.location.pathname],
includeReply: false
}).then(function (res) {
countELement.innerText = res[0].count
}).catch(function (err) {
console.error(err);
});
}
const runFn = () => {
init()
}
const loadTwikoo = () => {
if (typeof twikoo === 'object') {
setTimeout(runFn,0)
return
}
getScript('https://npm.elemecdn.com/[email protected]/dist/twikoo.all.min.js').then(runFn)
}
if ('Twikoo' === 'Twikoo' || !false) {
if (false) anzhiyu.loadComment(document.getElementById('twikoo-wrap'), loadTwikoo)
else loadTwikoo()
} else {
window.loadOtherComment = () => {
loadTwikoo()
}
}
})()</script><input type="hidden" name="page-type" id="page-type" value="post"></div><script>window.addEventListener('load', () => {
const changeContent = (content) => {
if (content === '') return content
content = content.replace(/<img.*?src="(.*?)"?[^\>]+>/ig, '[图片]') // replace image link
content = content.replace(/<a[^>]+?href=["']?([^"']+)["']?[^>]*>([^<]+)<\/a>/gi, '[链接]') // replace url
content = content.replace(/<pre><code>.*?<\/pre>/gi, '[代码]') // replace code
content = content.replace(/<[^>]+>/g,"") // remove html tag
if (content.length > 150) {
content = content.substring(0,150) + '...'
}
return content
}
const getComment = () => {
const runTwikoo = () => {
twikoo.getRecentComments({
envId: 'https://twikoo.beidaoaz.top/',
region: '',
pageSize: 6,
includeReply: true
}).then(function (res) {
const twikooArray = res.map(e => {
return {
'content': changeContent(e.comment),
'avatar': e.avatar,
'nick': e.nick,
'url': e.url + '#' + e.id,
'date': new Date(e.created).toISOString()
}
})
saveToLocal.set('twikoo-newest-comments', JSON.stringify(twikooArray), 10/(60*24))
generateHtml(twikooArray)
}).catch(function (err) {
const $dom = document.querySelector('#card-newest-comments .aside-list')
$dom.innerHTML= "无法获取评论,请确认相关配置是否正确"
})
}
if (typeof twikoo === 'object') {
runTwikoo()
} else {
getScript('https://npm.elemecdn.com/[email protected]/dist/twikoo.all.min.js').then(runTwikoo)
}
}
const generateHtml = array => {
let result = ''
if (array.length) {
for (let i = 0; i < array.length; i++) {
result += '<div class=\'aside-list-item\'>'
if (true) {
const name = 'data-lazy-src'
result += `<a href='${array[i].url}' class='thumbnail'><img ${name}='${array[i].avatar}' alt='${array[i].nick}'><div class='name'><span>${array[i].nick}</span></div></a>`
}
result += `<div class='content'>
<a class='comment' href='${array[i].url}' title='${array[i].content}'>${array[i].content}</a>
<time datetime="${array[i].date}">${anzhiyu.diffDate(array[i].date, true)}</time></div>
</div>`
}
} else {
result += '没有评论'
}
let $dom = document.querySelector('#card-newest-comments .aside-list')
$dom.innerHTML= result
window.lazyLoadInstance && window.lazyLoadInstance.update()
window.pjax && window.pjax.refresh($dom)
}
const newestCommentInit = () => {
if (document.querySelector('#card-newest-comments .aside-list')) {
const data = saveToLocal.get('twikoo-newest-comments')
if (data) {
generateHtml(JSON.parse(data))
} else {
getComment()
}
}
}
newestCommentInit()
document.addEventListener('pjax:complete', newestCommentInit)
})</script><script async data-pjax src="https://npm.elemecdn.com/[email protected]/bubble/bubble.js"></script><script data-pjax="true">if (document.querySelector(".comment-barrage")){
var commentBarrageConfig = {
maxBarrage: 1,
barrageTime: 4000,
twikooUrl: "https://twikoo.beidaoaz.top/",
accessToken: "",
pageUrl: window.location.pathname,
barrageTimer: [],
barrageList: [],
barrageIndex: 0,
dom: document.querySelector(".comment-barrage"),
};
var commentInterval = null;
var hoverOnCommentBarrage = false;
document.querySelector(".comment-barrage").addEventListener("mouseenter", function() {
hoverOnCommentBarrage = true;
console.log("热评悬浮");
});
document.querySelector(".comment-barrage").addEventListener("mouseleave", function() {
hoverOnCommentBarrage = false;
console.log("停止悬浮");
});
function initCommentBarrage() {
if (!commentBarrageConfig.dom) return;
var data = JSON.stringify({
event: "COMMENT_GET",
"commentBarrageConfig.accessToken": commentBarrageConfig.accessToken,
url: commentBarrageConfig.pageUrl,
});
var xhr = new XMLHttpRequest();
xhr.withCredentials = true;
xhr.addEventListener("readystatechange", function () {
if (this.readyState === 4 && this.responseText) {
commentBarrageConfig.barrageList = commentLinkFilter(JSON.parse(this.responseText).data);
commentBarrageConfig.dom.innerHTML = "";
}
});
xhr.open("POST", commentBarrageConfig.twikooUrl);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.send(data);
clearInterval(commentInterval);
commentInterval = null;
commentInterval = setInterval(() => {
if (commentBarrageConfig.barrageList.length && !hoverOnCommentBarrage) {
popCommentBarrage(commentBarrageConfig.barrageList[commentBarrageConfig.barrageIndex]);
commentBarrageConfig.barrageIndex += 1;
commentBarrageConfig.barrageIndex %= commentBarrageConfig.barrageList.length;
}
if (
commentBarrageConfig.barrageTimer.length >
(commentBarrageConfig.barrageList.length > commentBarrageConfig.maxBarrage
? commentBarrageConfig.maxBarrage
: commentBarrageConfig.barrageList.length) &&
!hoverOnCommentBarrage
) {
removeCommentBarrage(commentBarrageConfig.barrageTimer.shift());
}
}, commentBarrageConfig.barrageTime);
}
function commentLinkFilter(data) {
data.sort((a, b) => {
return a.created - b.created;
});
let newData = [];
data.forEach(item => {
newData.push(...getCommentReplies(item));
});
return newData;
}
function getCommentReplies(item) {
if (item.replies) {
let replies = [item];
item.replies.forEach(item => {
replies.push(...getCommentReplies(item));
});
return replies;
} else {
return [];
}
}
function popCommentBarrage(data) {
let barrage = document.createElement("div");
barrage.className = "comment-barrage-item";
barrage.innerHTML = `
<div class="barrageHead">
<a class="barrageTitle ${
data.mailMd5 === "d338f432ad0bf2f61e5fe4ad1642725d" ? "barrageBloggerTitle" : ""
}" href="javascript:anzhiyu.scrollTo('#post-comment')"">
${data.mailMd5 === "d338f432ad0bf2f61e5fe4ad1642725d" ? "博主" : "热评"}
</a>
<div class="barrageNick">${data.nick}</div>
<img class="nolazyload barrageAvatar" src="https://cravatar.cn/avatar/${data.mailMd5}"/>
<a class="comment-barrage-close" href="javascript:anzhiyu.switchCommentBarrage()" rel="external nofollow noreferrer"><i class="anzhiyufont anzhiyu-icon-xmark"></i></a>
</div>
<a class="barrageContent" href="#${data.id}">
<object>${data.comment}</object>
</a>
`;
commentBarrageConfig.barrageTimer.push(barrage);
commentBarrageConfig.dom.append(barrage);
}
function removeCommentBarrage(barrage) {
barrage.className = "comment-barrage-item out";
setTimeout(() => {
if (commentBarrageConfig.dom && commentBarrageConfig.dom.contains(barrage)) {
commentBarrageConfig.dom.removeChild(barrage);
}
}, 1000);
}
// 自动隐藏
const commentEntryCallback = (entries) => {
const commentBarrage = document.querySelector(".comment-barrage");
const postComment = document.getElementById("post-comment");
entries.forEach(entry => {
if (postComment && commentBarrage && document.body.clientWidth > 768) {
commentBarrage.style.bottom = entry.isIntersecting ? "-200px" : "0";
}
});
};
// 创建IntersectionObserver实例
const observer = new IntersectionObserver(commentEntryCallback, {
root: null,
rootMargin: "0px",
threshold: 0
});
// 监视目标元素
const postCommentTarget = document.getElementById("post-comment");
if (postCommentTarget) {
observer.observe(postCommentTarget);
}
initCommentBarrage();
if (localStorage.getItem("commentBarrageSwitch") !== "false") {
document.querySelector(".comment-barrage").style.display = "flex";
document.querySelector(".menu-commentBarrage-text").textContent = "关闭热评";
} else {
document.querySelector(".comment-barrage").style.display = "none";
document.querySelector(".menu-commentBarrage-text").textContent = "显示热评";
}
document.addEventListener("pjax:send", function () {
clearInterval(commentInterval);
});
}</script><script data-pjax src="https://npm.elemecdn.com/[email protected]/catalog-bar/catalog-bar.js"></script><script async data-pjax src="https://npm.elemecdn.com/[email protected]/categoryBar/categoryBar.js"></script><script async data-pjax src="https://npm.elemecdn.com/[email protected]/waterfall/waterfall.js"></script><script src="https://lf3-cdn-tos.bytecdntp.com/cdn/expire-1-M/qrcodejs/1.0.0/qrcode.min.js"></script><script>// 初始化函数
let rm = {};
//禁止图片拖拽
let imgElements = document.getElementsByTagName("img");
for (let i = 0; i < imgElements.length; i++) {
imgElements[i].addEventListener("dragstart", function (event) {
event.preventDefault();
});
}
// 显示菜单
rm.showRightMenu = function (isTrue, x = 0, y = 0) {
console.info(x, y)
let rightMenu = document.getElementById("rightMenu");
rightMenu.style.top = x + "px";
rightMenu.style.left = y + "px";
if (isTrue) {
rightMenu.style.display = "block";
stopMaskScroll();
} else {
rightMenu.style.display = "none";
}
};
// 隐藏菜单
rm.hideRightMenu = function () {
rm.showRightMenu(false);
let rightMenuMask = document.querySelector("#rightmenu-mask");
rightMenuMask.style.display = "none";
};
// 尺寸
let rmWidth = document.getElementById("rightMenu").offsetWidth;
let rmHeight = document.getElementById("rightMenu").offsetHeight;
// 重新定义尺寸
rm.reloadrmSize = function () {
rightMenu.style.visibility = "hidden";
rightMenu.style.display = "block";
// 获取宽度和高度
rmWidth = document.getElementById("rightMenu").offsetWidth;
rmHeight = document.getElementById("rightMenu").offsetHeight;
rightMenu.style.visibility = "visible";
};
// 获取点击的href
let domhref = "";
let domImgSrc = "";
let globalEvent = null;
var oncontextmenuFunction = function (event) {
if (document.body.clientWidth > 768) {
let pageX = event.clientX + 10; //加10是为了防止显示时鼠标遮在菜单上
let pageY = event.clientY;
//其他额外菜单
const $rightMenuOther = document.querySelector(".rightMenuOther");
const $rightMenuPlugin = document.querySelector(".rightMenuPlugin");
const $rightMenuCopyText = document.querySelector("#menu-copytext");
const $rightMenuPasteText = document.querySelector("#menu-pastetext");
const $rightMenuCommentText = document.querySelector("#menu-commenttext");
const $rightMenuNewWindow = document.querySelector("#menu-newwindow");
const $rightMenuNewWindowImg = document.querySelector("#menu-newwindowimg");
const $rightMenuCopyLink = document.querySelector("#menu-copylink");
const $rightMenuCopyImg = document.querySelector("#menu-copyimg");
const $rightMenuDownloadImg = document.querySelector("#menu-downloadimg");
const $rightMenuSearch = document.querySelector("#menu-search");
const $rightMenuSearchBaidu = document.querySelector("#menu-searchBaidu");
const $rightMenuMusicToggle = document.querySelector("#menu-music-toggle");
const $rightMenuMusicBack = document.querySelector("#menu-music-back");
const $rightMenuMusicForward = document.querySelector("#menu-music-forward");
const $rightMenuMusicPlaylist = document.querySelector("#menu-music-playlist");
const $rightMenuMusicCopyMusicName = document.querySelector("#menu-music-copyMusicName");
let href = event.target.href;
let imgsrc = event.target.currentSrc;
// 判断模式 扩展模式为有事件
let pluginMode = false;
$rightMenuOther.style.display = "block";
globalEvent = event;
// 检查是否需要复制 是否有选中文本
if (selectTextNow && window.getSelection()) {
pluginMode = true;
$rightMenuCopyText.style.display = "block";
$rightMenuCommentText.style.display = "block";
$rightMenuSearch.style.display = "block";
$rightMenuSearchBaidu.style.display = "block";
} else {
$rightMenuCopyText.style.display = "none";
$rightMenuCommentText.style.display = "none";
$rightMenuSearchBaidu.style.display = "none";
$rightMenuSearch.style.display = "none";
}
//检查是否右键点击了链接a标签
if (href) {
pluginMode = true;
$rightMenuNewWindow.style.display = "block";
$rightMenuCopyLink.style.display = "block";
domhref = href;
} else {
$rightMenuNewWindow.style.display = "none";
$rightMenuCopyLink.style.display = "none";
}
//检查是否需要复制图片
if (imgsrc) {
pluginMode = true;
$rightMenuCopyImg.style.display = "block";
$rightMenuDownloadImg.style.display = "block";
$rightMenuNewWindowImg.style.display = "block";
document.getElementById("rightMenu").style.width="12rem"
domImgSrc = imgsrc;
} else {
$rightMenuCopyImg.style.display = "none";
$rightMenuDownloadImg.style.display = "none";
$rightMenuNewWindowImg.style.display = "none";
}
// 判断是否为输入框
if (event.target.tagName.toLowerCase() === "input" || event.target.tagName.toLowerCase() === "textarea") {
pluginMode = true;
$rightMenuPasteText.style.display = "block";
} else {
$rightMenuPasteText.style.display = "none";
}
const navMusicEl = document.querySelector("#nav-music");
//判断是否是音乐
if (navMusicEl && navMusicEl.contains(event.target)) {
pluginMode = true;
$rightMenuMusicToggle.style.display = "block";
$rightMenuMusicBack.style.display = "block";
$rightMenuMusicForward.style.display = "block";
$rightMenuMusicPlaylist.style.display = "block";
$rightMenuMusicCopyMusicName.style.display = "block";
} else {
$rightMenuMusicToggle.style.display = "none";
$rightMenuMusicBack.style.display = "none";
$rightMenuMusicForward.style.display = "none";
$rightMenuMusicPlaylist.style.display = "none";
$rightMenuMusicCopyMusicName.style.display = "none";
}
// 如果不是扩展模式则隐藏扩展模块
if (pluginMode) {
$rightMenuOther.style.display = "none";
$rightMenuPlugin.style.display = "block";
} else {
$rightMenuPlugin.style.display = "none";
}
rm.reloadrmSize();
// 鼠标默认显示在鼠标右下方,当鼠标靠右或靠下时,将菜单显示在鼠标左方\上方
if (pageX + rmWidth > window.innerWidth) {
pageX -= rmWidth + 10;
}
if (pageY + rmHeight > window.innerHeight) {
pageY -= pageY + rmHeight - window.innerHeight;
}
rm.showRightMenu(true, pageY, pageX);
document.getElementById("rightmenu-mask").style.display = "flex";
return false;
}
};
// 监听右键初始化
window.oncontextmenu = oncontextmenuFunction
// 下载图片状态
rm.downloadimging = false;
// 复制图片到剪贴板
rm.writeClipImg = function (imgsrc) {
console.log("按下复制");
rm.hideRightMenu();
anzhiyu.snackbarShow("正在下载中,请稍后", false, 10000);
if (rm.downloadimging == false) {
rm.downloadimging = true;
setTimeout(function () {
copyImage(imgsrc);
anzhiyu.snackbarShow("复制成功!图片已添加盲水印,请遵守版权协议");
rm.downloadimging = false;
}, "10000");
}
};
function imageToBlob(imageURL) {
const img = new Image();