《C语言》—— 数组详解

2019/04/10 10:10
阅读数 9

<div class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; word-spacing: 0px; letter-spacing: 0px; font-family: 'Helvetica Neue', Helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', Arial, sans-serif; background-image: linear-gradient(90deg, rgba(50, 0, 0, 0.05) 3%, rgba(0, 0, 0, 0) 3%), linear-gradient(360deg, rgba(50, 0, 0, 0.05) 3%, rgba(0, 0, 0, 0) 3%); background-size: 20px 20px; background-position: center center;"><blockquote style="line-height: inherit; display: block; padding: 15px 15px 15px 1rem; font-size: 0.9em; margin: 1em 0px; color: rgb(129, 145, 152); border-left: 6px solid rgb(220, 230, 240); background: rgb(242, 247, 251); overflow: auto; overflow-wrap: normal; word-break: normal;"> <p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 0px;">书籍使我变成了一个幸福的人,使我的生活变成轻松而舒适的诗。——高尔基</p> </blockquote> <p align="center" style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">本文已经收录至我的GitHub,欢迎大家踊跃star 和 issues。</p> <h4 align="center" style="color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px; font-weight: bold; font-size: 1.2em;"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;"><a href="https://github.com/midou-tech/articles" target="_blank" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; text-decoration: none; color: rgb(30, 107, 184); overflow-wrap: break-word;">https://github.com/midou-tech/articles</a></span></h4> <p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;"></p><h4 style="line-height: inherit; padding: 0px; margin: 1.5em 0px; font-weight: bold; font-size: 1.2em; color: red; text-align: center;"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;">点关注,不迷路!!! </span></h4><p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;"></p> <h3 id="h" style="color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px; font-weight: bold; font-size: 1.3em;"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;">前言</span></h3> <p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;"> 我本来准备C语言章节就写个指针就ok了,在我看来C语言的精华部分就是指针了。但是有很多同学就开始在群里各种拉扯C语言的其他问题,没办法,我是龙叔嘛,想想还是整理一下,把一些重要的C语言知识点都一一更新了吧。C语言指针的内容请点击 <a href="https://mp.weixin.qq.com/s/tdyCTqH9WYMrS0HmRtVPng" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; text-decoration: none; color: rgb(30, 107, 184); overflow-wrap: break-word;">指针(上)</a> 和 <a href="https://mp.weixin.qq.com/s/-C_PHEk0ZUf7JUN0Bd80rQ" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; text-decoration: none; color: rgb(30, 107, 184); overflow-wrap: break-word;">指针(下)</a>,<strong style="font-size: inherit; line-height: inherit; font-weight: bold; overflow-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0px 2px; color: rgb(233, 105, 0); background: rgb(248, 248, 248);">记得点关注,不迷路</strong></p> <h3 id="h-1" style="color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px; font-weight: bold; font-size: 1.3em;"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;">数组的基本概念</span></h3> <p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;"> 我们把一组数据的集合称为<strong style="font-size: inherit; line-height: inherit; font-weight: bold; overflow-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0px 2px; color: rgb(233, 105, 0); background: rgb(248, 248, 248);">数组(Array)</strong>,它所包含的每一个数据叫做<strong style="font-size: inherit; line-height: inherit; font-weight: bold; overflow-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0px 2px; color: rgb(233, 105, 0); background: rgb(248, 248, 248);">数组元素</strong>(Element),所包含的数据的个数称为<strong style="font-size: inherit; line-height: inherit; font-weight: bold; overflow-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0px 2px; color: rgb(233, 105, 0); background: rgb(248, 248, 248);">数组长度</strong>(Length),数组中的每个元素都有一个序号,这个序号从0开始,而不是从我们熟悉的1开始,称为<strong style="font-size: inherit; line-height: inherit; font-weight: bold; overflow-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0px 2px; color: rgb(233, 105, 0); background: rgb(248, 248, 248);">下标</strong>(Index),所包含数组的里面元素的类型叫做<strong style="font-size: inherit; line-height: inherit; font-weight: bold; overflow-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0px 2px; color: rgb(233, 105, 0); background: rgb(248, 248, 248);">数组类型</strong>(Type)。</p> <p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;"> 一句话就说清楚了数组的基本概念,就是这么简单,^_^。</p> <h3 id="h-2" style="color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px; font-weight: bold; font-size: 1.3em;"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;">数组底层结构探析</span></h3> <pre style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;"><code class="c language-c hljs cpp" style="overflow-wrap: break-word; margin: 0px 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; overflow-x: auto; padding: 0.5em; background: rgb(30, 30, 30); color: rgb(220, 220, 220); white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;"><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">1</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(86, 156, 214); word-wrap: inherit !important; word-break: inherit !important;">int</span>&nbsp;<span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(78, 201, 176); word-wrap: inherit !important; word-break: inherit !important;">array</span>[<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); word-wrap: inherit !important; word-break: inherit !important;">5</span>];<br></code></pre> <p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;"><strong style="font-size: inherit; line-height: inherit; font-weight: bold; overflow-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0px 2px; color: rgb(233, 105, 0); background: rgb(248, 248, 248);">内存布局图</strong></p> <figure style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;"><img src="https://tva1.sinaimg.cn/large/0082zybply1gc0twah5dmj30jm06ujrj.jpg" alt="" title="" style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; display: block; margin: 0px auto; max-width: 100%;"><figcaption style="line-height: inherit; margin: 0px; padding: 0px; margin-top: 10px; text-align: center; color: rgb(153, 153, 153); font-size: 0.7em;"></figcaption></figure> <p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;"> 不要看这个图简单,底层就是这样的。数组是一个整体,它的内存是<strong style="font-size: inherit; line-height: inherit; font-weight: bold; overflow-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0px 2px; color: rgb(233, 105, 0); background: rgb(248, 248, 248);">连续</strong>的;也就是说,数组元素之间是相互挨着的,彼此之间没有一点点缝隙。</p> <p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;"> <strong style="font-size: inherit; line-height: inherit; font-weight: bold; overflow-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0px 2px; color: rgb(233, 105, 0); background: rgb(248, 248, 248);">这一点很重要,连续的内存为<a href="https://mp.weixin.qq.com/s/tdyCTqH9WYMrS0HmRtVPng" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; text-decoration: none; color: rgb(30, 107, 184); overflow-wrap: break-word;">指针</a>操作(通过指针来访问数组元素)和内存处理(整块内存的复制、写入等)提供了便利,这使得数组可以作为缓存使用。</strong></p> <p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;"> 有同学估计要说什么叫做指针操作,听龙叔絮叨下。</p> <pre style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;"><code class="c language-c hljs cpp" style="overflow-wrap: break-word; margin: 0px 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; overflow-x: auto; padding: 0.5em; background: rgb(30, 30, 30); color: rgb(220, 220, 220); white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;"><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">1</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(86, 156, 214); word-wrap: inherit !important; word-break: inherit !important;">int</span>&nbsp;arr[<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); word-wrap: inherit !important; word-break: inherit !important;">5</span>]&nbsp;=&nbsp;{<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); word-wrap: inherit !important; word-break: inherit !important;">1</span>,<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); word-wrap: inherit !important; word-break: inherit !important;">2</span>,<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); word-wrap: inherit !important; word-break: inherit !important;">3</span>,<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); word-wrap: inherit !important; word-break: inherit !important;">4</span>,<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); word-wrap: inherit !important; word-break: inherit !important;">5</span>};<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">2</span><span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(78, 201, 176); word-wrap: inherit !important; word-break: inherit !important;">printf</span>(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(214, 157, 133); word-wrap: inherit !important; word-break: inherit !important;">"%d\n"</span>,arr[<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); word-wrap: inherit !important; word-break: inherit !important;">3</span>]);<br></code></pre> <p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;"> 看上面的代码,学过数组都知道arr[3]是取第三个元素的值,<strong style="font-size: inherit; line-height: inherit; font-weight: bold; overflow-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0px 2px; color: rgb(233, 105, 0); background: rgb(248, 248, 248);">那我就要问你了,怎么取到值的呢???</strong></p> <p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;"> 不要慌,龙叔告诉你其实就是指针操作。当我们声明并定义数组<code style="font-size: inherit; line-height: inherit; overflow-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0px 2px; color: rgb(233, 105, 0); background: rgb(248, 248, 248);">int arr[5] = {1,2,3,4,5}</code>,此时数组被分配了5个int大小的空间在<strong style="font-size: inherit; line-height: inherit; font-weight: bold; overflow-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0px 2px; color: rgb(233, 105, 0); background: rgb(248, 248, 248);">栈上</strong>,并初始化了数组元素。我们都知道数组名代表数组的首元素的首地址,那么很明显就可以得到<code style="font-size: inherit; line-height: inherit; overflow-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0px 2px; color: rgb(233, 105, 0); background: rgb(248, 248, 248);">arr[3] = arr + 3</code>。指针的加减操作详情请看龙叔公号,微信搜索 <strong style="font-size: inherit; line-height: inherit; font-weight: bold; overflow-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0px 2px; color: rgb(233, 105, 0); background: rgb(248, 248, 248);">龙跃十二</strong> 即可订阅喔。</p> <figure style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;"><img src="https://tva1.sinaimg.cn/large/0082zybply1gc0ubjecudj30vo0cmgmi.jpg" alt="" title="" style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; display: block; margin: 0px auto; max-width: 100%;"><figcaption style="line-height: inherit; margin: 0px; padding: 0px; margin-top: 10px; text-align: center; color: rgb(153, 153, 153); font-size: 0.7em;"></figcaption></figure> <h3 id="h-3" style="color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px; font-weight: bold; font-size: 1.3em;"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;">数组的运算</span></h3> <pre style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;"><code class="c language-c hljs cpp" style="overflow-wrap: break-word; margin: 0px 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; overflow-x: auto; padding: 0.5em; background: rgb(30, 30, 30); color: rgb(220, 220, 220); white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;"><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;"> 1</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(86, 156, 214); word-wrap: inherit !important; word-break: inherit !important;">int</span>&nbsp;a[]&nbsp;=&nbsp;{<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); word-wrap: inherit !important; word-break: inherit !important;">1</span>,<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); word-wrap: inherit !important; word-break: inherit !important;">2</span>,<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); word-wrap: inherit !important; word-break: inherit !important;">3</span>,<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); word-wrap: inherit !important; word-break: inherit !important;">4</span>};<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;"> 2</span><span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(78, 201, 176); word-wrap: inherit !important; word-break: inherit !important;">printf</span>(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(214, 157, 133); word-wrap: inherit !important; word-break: inherit !important;">"%d\n"</span>,<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(86, 156, 214); word-wrap: inherit !important; word-break: inherit !important;">sizeof</span>(a));<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;"> 3</span><span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(78, 201, 176); word-wrap: inherit !important; word-break: inherit !important;">printf</span>(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(214, 157, 133); word-wrap: inherit !important; word-break: inherit !important;">"%d\n"</span>,<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(86, 156, 214); word-wrap: inherit !important; word-break: inherit !important;">sizeof</span>(a+<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); word-wrap: inherit !important; word-break: inherit !important;">0</span>));<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;"> 4</span><span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(78, 201, 176); word-wrap: inherit !important; word-break: inherit !important;">printf</span>(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(214, 157, 133); word-wrap: inherit !important; word-break: inherit !important;">"%d\n"</span>,<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(86, 156, 214); word-wrap: inherit !important; word-break: inherit !important;">sizeof</span>(*a));<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;"> 5</span><span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(78, 201, 176); word-wrap: inherit !important; word-break: inherit !important;">printf</span>(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(214, 157, 133); word-wrap: inherit !important; word-break: inherit !important;">"%d\n"</span>,<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(86, 156, 214); word-wrap: inherit !important; word-break: inherit !important;">sizeof</span>(a+<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); word-wrap: inherit !important; word-break: inherit !important;">1</span>));<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;"> 6</span><span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(78, 201, 176); word-wrap: inherit !important; word-break: inherit !important;">printf</span>(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(214, 157, 133); word-wrap: inherit !important; word-break: inherit !important;">"%d\n"</span>,<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(86, 156, 214); word-wrap: inherit !important; word-break: inherit !important;">sizeof</span>(a[<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); word-wrap: inherit !important; word-break: inherit !important;">1</span>]));<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;"> 7</span><span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(78, 201, 176); word-wrap: inherit !important; word-break: inherit !important;">printf</span>(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(214, 157, 133); word-wrap: inherit !important; word-break: inherit !important;">"%d\n"</span>,<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(86, 156, 214); word-wrap: inherit !important; word-break: inherit !important;">sizeof</span>(&amp;a));<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;"> 8</span><span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(78, 201, 176); word-wrap: inherit !important; word-break: inherit !important;">printf</span>(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(214, 157, 133); word-wrap: inherit !important; word-break: inherit !important;">"%d\n"</span>,<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(86, 156, 214); word-wrap: inherit !important; word-break: inherit !important;">sizeof</span>(&amp;a+<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); word-wrap: inherit !important; word-break: inherit !important;">1</span>));<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;"> 9</span><span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(78, 201, 176); word-wrap: inherit !important; word-break: inherit !important;">printf</span>(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(214, 157, 133); word-wrap: inherit !important; word-break: inherit !important;">"%d\n"</span>,<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(86, 156, 214); word-wrap: inherit !important; word-break: inherit !important;">sizeof</span>(&amp;a[<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); word-wrap: inherit !important; word-break: inherit !important;">0</span>]));<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">10</span><span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(78, 201, 176); word-wrap: inherit !important; word-break: inherit !important;">printf</span>(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(214, 157, 133); word-wrap: inherit !important; word-break: inherit !important;">"%d\n"</span>,<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(86, 156, 214); word-wrap: inherit !important; word-break: inherit !important;">sizeof</span>(&amp;a[<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); word-wrap: inherit !important; word-break: inherit !important;">0</span>]+<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); word-wrap: inherit !important; word-break: inherit !important;">1</span>));<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">11</span><span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(87, 166, 74); font-style: italic; word-wrap: inherit !important; word-break: inherit !important;">//字符数组</span><br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">12</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(86, 156, 214); word-wrap: inherit !important; word-break: inherit !important;">char</span>&nbsp;arr[]&nbsp;=&nbsp;{<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(214, 157, 133); word-wrap: inherit !important; word-break: inherit !important;">'a'</span>,<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(214, 157, 133); word-wrap: inherit !important; word-break: inherit !important;">'b'</span>,<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(214, 157, 133); word-wrap: inherit !important; word-break: inherit !important;">'c'</span>,<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(214, 157, 133); word-wrap: inherit !important; word-break: inherit !important;">'d'</span>,<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(214, 157, 133); word-wrap: inherit !important; word-break: inherit !important;">'e'</span>,<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(214, 157, 133); word-wrap: inherit !important; word-break: inherit !important;">'f'</span>};<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">13</span><span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(78, 201, 176); word-wrap: inherit !important; word-break: inherit !important;">printf</span>(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(214, 157, 133); word-wrap: inherit !important; word-break: inherit !important;">"%d\n"</span>,&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(86, 156, 214); word-wrap: inherit !important; word-break: inherit !important;">sizeof</span>(arr));<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">14</span><span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(78, 201, 176); word-wrap: inherit !important; word-break: inherit !important;">printf</span>(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(214, 157, 133); word-wrap: inherit !important; word-break: inherit !important;">"%d\n"</span>,&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(86, 156, 214); word-wrap: inherit !important; word-break: inherit !important;">sizeof</span>(arr+<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); word-wrap: inherit !important; word-break: inherit !important;">0</span>));<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">15</span><span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(78, 201, 176); word-wrap: inherit !important; word-break: inherit !important;">printf</span>(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(214, 157, 133); word-wrap: inherit !important; word-break: inherit !important;">"%d\n"</span>,&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(86, 156, 214); word-wrap: inherit !important; word-break: inherit !important;">sizeof</span>(*arr));<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">16</span><span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(78, 201, 176); word-wrap: inherit !important; word-break: inherit !important;">printf</span>(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(214, 157, 133); word-wrap: inherit !important; word-break: inherit !important;">"%d\n"</span>,&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(86, 156, 214); word-wrap: inherit !important; word-break: inherit !important;">sizeof</span>(arr[<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); word-wrap: inherit !important; word-break: inherit !important;">1</span>]));<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">17</span><span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(78, 201, 176); word-wrap: inherit !important; word-break: inherit !important;">printf</span>(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(214, 157, 133); word-wrap: inherit !important; word-break: inherit !important;">"%d\n"</span>,&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(86, 156, 214); word-wrap: inherit !important; word-break: inherit !important;">sizeof</span>(&amp;arr));<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">18</span><span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(78, 201, 176); word-wrap: inherit !important; word-break: inherit !important;">printf</span>(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(214, 157, 133); word-wrap: inherit !important; word-break: inherit !important;">"%d\n"</span>,&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(86, 156, 214); word-wrap: inherit !important; word-break: inherit !important;">sizeof</span>(&amp;arr+<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); word-wrap: inherit !important; word-break: inherit !important;">1</span>));<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">19</span><span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(78, 201, 176); word-wrap: inherit !important; word-break: inherit !important;">printf</span>(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(214, 157, 133); word-wrap: inherit !important; word-break: inherit !important;">"%d\n"</span>,&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(86, 156, 214); word-wrap: inherit !important; word-break: inherit !important;">sizeof</span>(&amp;arr[<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); word-wrap: inherit !important; word-break: inherit !important;">0</span>]+<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); word-wrap: inherit !important; word-break: inherit !important;">1</span>));<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">20</span><span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(78, 201, 176); word-wrap: inherit !important; word-break: inherit !important;">printf</span>(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(214, 157, 133); word-wrap: inherit !important; word-break: inherit !important;">"%d\n"</span>,&nbsp;<span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(78, 201, 176); word-wrap: inherit !important; word-break: inherit !important;">strlen</span>(arr));<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">21</span><span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(78, 201, 176); word-wrap: inherit !important; word-break: inherit !important;">printf</span>(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(214, 157, 133); word-wrap: inherit !important; word-break: inherit !important;">"%d\n"</span>,&nbsp;<span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(78, 201, 176); word-wrap: inherit !important; word-break: inherit !important;">strlen</span>(arr+<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); word-wrap: inherit !important; word-break: inherit !important;">0</span>));<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">22</span><span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(78, 201, 176); word-wrap: inherit !important; word-break: inherit !important;">printf</span>(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(214, 157, 133); word-wrap: inherit !important; word-break: inherit !important;">"%d\n"</span>,&nbsp;<span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(78, 201, 176); word-wrap: inherit !important; word-break: inherit !important;">strlen</span>(*arr));<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">23</span><span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(78, 201, 176); word-wrap: inherit !important; word-break: inherit !important;">printf</span>(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(214, 157, 133); word-wrap: inherit !important; word-break: inherit !important;">"%d\n"</span>,&nbsp;<span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(78, 201, 176); word-wrap: inherit !important; word-break: inherit !important;">strlen</span>(arr[<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); word-wrap: inherit !important; word-break: inherit !important;">1</span>]));<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">24</span><span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(78, 201, 176); word-wrap: inherit !important; word-break: inherit !important;">printf</span>(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(214, 157, 133); word-wrap: inherit !important; word-break: inherit !important;">"%d\n"</span>,&nbsp;<span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(78, 201, 176); word-wrap: inherit !important; word-break: inherit !important;">strlen</span>(&amp;arr));<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">25</span><span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(78, 201, 176); word-wrap: inherit !important; word-break: inherit !important;">printf</span>(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(214, 157, 133); word-wrap: inherit !important; word-break: inherit !important;">"%d\n"</span>,&nbsp;<span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(78, 201, 176); word-wrap: inherit !important; word-break: inherit !important;">strlen</span>(&amp;arr+<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); word-wrap: inherit !important; word-break: inherit !important;">1</span>));<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">26</span><span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(78, 201, 176); word-wrap: inherit !important; word-break: inherit !important;">printf</span>(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(214, 157, 133); word-wrap: inherit !important; word-break: inherit !important;">"%d\n"</span>,&nbsp;<span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(78, 201, 176); word-wrap: inherit !important; word-break: inherit !important;">strlen</span>(&amp;arr[<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); word-wrap: inherit !important; word-break: inherit !important;">0</span>]+<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); word-wrap: inherit !important; word-break: inherit !important;">1</span>));<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">27</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(86, 156, 214); word-wrap: inherit !important; word-break: inherit !important;">char</span>&nbsp;*p&nbsp;=&nbsp;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(214, 157, 133); word-wrap: inherit !important; word-break: inherit !important;">"abcdef"</span>;<span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(78, 201, 176); word-wrap: inherit !important; word-break: inherit !important;">printf</span>(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(214, 157, 133); word-wrap: inherit !important; word-break: inherit !important;">"%d\n"</span>,&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(86, 156, 214); word-wrap: inherit !important; word-break: inherit !important;">sizeof</span>(p));<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">28</span><span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(78, 201, 176); word-wrap: inherit !important; word-break: inherit !important;">printf</span>(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(214, 157, 133); word-wrap: inherit !important; word-break: inherit !important;">"%d\n"</span>,&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(86, 156, 214); word-wrap: inherit !important; word-break: inherit !important;">sizeof</span>(p+<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); word-wrap: inherit !important; word-break: inherit !important;">1</span>));<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">29</span><span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(78, 201, 176); word-wrap: inherit !important; word-break: inherit !important;">printf</span>(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(214, 157, 133); word-wrap: inherit !important; word-break: inherit !important;">"%d\n"</span>,&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(86, 156, 214); word-wrap: inherit !important; word-break: inherit !important;">sizeof</span>(*p));<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">30</span><span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(78, 201, 176); word-wrap: inherit !important; word-break: inherit !important;">printf</span>(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(214, 157, 133); word-wrap: inherit !important; word-break: inherit !important;">"%d\n"</span>,&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(86, 156, 214); word-wrap: inherit !important; word-break: inherit !important;">sizeof</span>(p[<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); word-wrap: inherit !important; word-break: inherit !important;">0</span>]));<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">31</span><span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(78, 201, 176); word-wrap: inherit !important; word-break: inherit !important;">printf</span>(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(214, 157, 133); word-wrap: inherit !important; word-break: inherit !important;">"%d\n"</span>,&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(86, 156, 214); word-wrap: inherit !important; word-break: inherit !important;">sizeof</span>(&amp;p));<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">32</span><span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(78, 201, 176); word-wrap: inherit !important; word-break: inherit !important;">printf</span>(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(214, 157, 133); word-wrap: inherit !important; word-break: inherit !important;">"%d\n"</span>,&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(86, 156, 214); word-wrap: inherit !important; word-break: inherit !important;">sizeof</span>(&amp;p+<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); word-wrap: inherit !important; word-break: inherit !important;">1</span>));<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">33</span><span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(78, 201, 176); word-wrap: inherit !important; word-break: inherit !important;">printf</span>(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(214, 157, 133); word-wrap: inherit !important; word-break: inherit !important;">"%d\n"</span>,&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(86, 156, 214); word-wrap: inherit !important; word-break: inherit !important;">sizeof</span>(&amp;p[<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); word-wrap: inherit !important; word-break: inherit !important;">0</span>]+<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); word-wrap: inherit !important; word-break: inherit !important;">1</span>));<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">34</span><span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(78, 201, 176); word-wrap: inherit !important; word-break: inherit !important;">printf</span>(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(214, 157, 133); word-wrap: inherit !important; word-break: inherit !important;">"%d\n"</span>,&nbsp;<span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(78, 201, 176); word-wrap: inherit !important; word-break: inherit !important;">strlen</span>(p));<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">35</span><span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(78, 201, 176); word-wrap: inherit !important; word-break: inherit !important;">printf</span>(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(214, 157, 133); word-wrap: inherit !important; word-break: inherit !important;">"%d\n"</span>,&nbsp;<span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(78, 201, 176); word-wrap: inherit !important; word-break: inherit !important;">strlen</span>(p+<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); word-wrap: inherit !important; word-break: inherit !important;">1</span>));<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">36</span><span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(78, 201, 176); word-wrap: inherit !important; word-break: inherit !important;">printf</span>(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(214, 157, 133); word-wrap: inherit !important; word-break: inherit !important;">"%d\n"</span>,&nbsp;<span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(78, 201, 176); word-wrap: inherit !important; word-break: inherit !important;">strlen</span>(*p));<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">37</span><span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(78, 201, 176); word-wrap: inherit !important; word-break: inherit !important;">printf</span>(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(214, 157, 133); word-wrap: inherit !important; word-break: inherit !important;">"%d\n"</span>,&nbsp;<span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(78, 201, 176); word-wrap: inherit !important; word-break: inherit !important;">strlen</span>(p[<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); word-wrap: inherit !important; word-break: inherit !important;">0</span>]));<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">38</span><span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(78, 201, 176); word-wrap: inherit !important; word-break: inherit !important;">printf</span>(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(214, 157, 133); word-wrap: inherit !important; word-break: inherit !important;">"%d\n"</span>,&nbsp;<span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(78, 201, 176); word-wrap: inherit !important; word-break: inherit !important;">strlen</span>(&amp;p));<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">39</span><span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(78, 201, 176); word-wrap: inherit !important; word-break: inherit !important;">printf</span>(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(214, 157, 133); word-wrap: inherit !important; word-break: inherit !important;">"%d\n"</span>,&nbsp;<span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(78, 201, 176); word-wrap: inherit !important; word-break: inherit !important;">strlen</span>(&amp;p+<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); word-wrap: inherit !important; word-break: inherit !important;">1</span>));<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">40</span><span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(78, 201, 176); word-wrap: inherit !important; word-break: inherit !important;">printf</span>(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(214, 157, 133); word-wrap: inherit !important; word-break: inherit !important;">"%d\n"</span>,&nbsp;<span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(78, 201, 176); word-wrap: inherit !important; word-break: inherit !important;">strlen</span>(&amp;p[<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); word-wrap: inherit !important; word-break: inherit !important;">0</span>]+<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); word-wrap: inherit !important; word-break: inherit !important;">1</span>));<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">41</span><span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(87, 166, 74); font-style: italic; word-wrap: inherit !important; word-break: inherit !important;">//二维数组</span><br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">42</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(86, 156, 214); word-wrap: inherit !important; word-break: inherit !important;">int</span>&nbsp;a[<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); word-wrap: inherit !important; word-break: inherit !important;">3</span>][<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); word-wrap: inherit !important; word-break: inherit !important;">4</span>]&nbsp;=&nbsp;{<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); word-wrap: inherit !important; word-break: inherit !important;">0</span>};<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">43</span><span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(78, 201, 176); word-wrap: inherit !important; word-break: inherit !important;">printf</span>(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(214, 157, 133); word-wrap: inherit !important; word-break: inherit !important;">"%d\n"</span>,<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(86, 156, 214); word-wrap: inherit !important; word-break: inherit !important;">sizeof</span>(a));<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">44</span><span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(78, 201, 176); word-wrap: inherit !important; word-break: inherit !important;">printf</span>(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(214, 157, 133); word-wrap: inherit !important; word-break: inherit !important;">"%d\n"</span>,<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(86, 156, 214); word-wrap: inherit !important; word-break: inherit !important;">sizeof</span>(a[<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); word-wrap: inherit !important; word-break: inherit !important;">0</span>][<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); word-wrap: inherit !important; word-break: inherit !important;">0</span>]));<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">45</span><span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(78, 201, 176); word-wrap: inherit !important; word-break: inherit !important;">printf</span>(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(214, 157, 133); word-wrap: inherit !important; word-break: inherit !important;">"%d\n"</span>,<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(86, 156, 214); word-wrap: inherit !important; word-break: inherit !important;">sizeof</span>(a[<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); word-wrap: inherit !important; word-break: inherit !important;">0</span>]));<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">46</span><span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(78, 201, 176); word-wrap: inherit !important; word-break: inherit !important;">printf</span>(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(214, 157, 133); word-wrap: inherit !important; word-break: inherit !important;">"%d\n"</span>,<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(86, 156, 214); word-wrap: inherit !important; word-break: inherit !important;">sizeof</span>(a[<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); word-wrap: inherit !important; word-break: inherit !important;">0</span>]+<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); word-wrap: inherit !important; word-break: inherit !important;">1</span>));<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">47</span><span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(78, 201, 176); word-wrap: inherit !important; word-break: inherit !important;">printf</span>(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(214, 157, 133); word-wrap: inherit !important; word-break: inherit !important;">"%d\n"</span>,<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(86, 156, 214); word-wrap: inherit !important; word-break: inherit !important;">sizeof</span>(a+<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); word-wrap: inherit !important; word-break: inherit !important;">1</span>));<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">48</span><span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(78, 201, 176); word-wrap: inherit !important; word-break: inherit !important;">printf</span>(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(214, 157, 133); word-wrap: inherit !important; word-break: inherit !important;">"%d\n"</span>,<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(86, 156, 214); word-wrap: inherit !important; word-break: inherit !important;">sizeof</span>(&amp;a[<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); word-wrap: inherit !important; word-break: inherit !important;">0</span>]+<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); word-wrap: inherit !important; word-break: inherit !important;">1</span>));<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">49</span><span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(78, 201, 176); word-wrap: inherit !important; word-break: inherit !important;">printf</span>(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(214, 157, 133); word-wrap: inherit !important; word-break: inherit !important;">"%d\n"</span>,<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(86, 156, 214); word-wrap: inherit !important; word-break: inherit !important;">sizeof</span>(*a));<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">50</span><span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(78, 201, 176); word-wrap: inherit !important; word-break: inherit !important;">printf</span>(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(214, 157, 133); word-wrap: inherit !important; word-break: inherit !important;">"%d\n"</span>,<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(86, 156, 214); word-wrap: inherit !important; word-break: inherit !important;">sizeof</span>(a[<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); word-wrap: inherit !important; word-break: inherit !important;">3</span>]));<br></code></pre> <p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">就这几个运算,估计会难倒很多同学的,不信你可以把答案写出来之后在去跑一遍,<strong style="font-size: inherit; line-height: inherit; font-weight: bold; overflow-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0px 2px; color: rgb(233, 105, 0); background: rgb(248, 248, 248);">全对找我拿红包</strong>。</p> <figure style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;"><img src="https://tva1.sinaimg.cn/large/0082zybply1gc0x87p5iuj30730730sq.jpg" alt="" title="" style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; display: block; margin: 0px auto; max-width: 100%;"><figcaption style="line-height: inherit; margin: 0px; padding: 0px; margin-top: 10px; text-align: center; color: rgb(153, 153, 153); font-size: 0.7em;"></figcaption></figure> <p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;"><strong style="font-size: inherit; line-height: inherit; font-weight: bold; overflow-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0px 2px; color: rgb(233, 105, 0); background: rgb(248, 248, 248);">sizeof(数组名),代表整个数组的字节数;&amp;数组名,代表取得整个数组的地址。</strong></p> <h3 id="h-4" style="color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px; font-weight: bold; font-size: 1.3em;"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;">数组的一些特性</span></h3> <ol style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; padding-left: 32px; list-style-type: decimal;"> <li style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; margin-bottom: 0.5em;"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;">严格上说数组只有一维数组。n维数组是在一维数组里面存放一个(n-1)维数组,掌握以为数组即可。</span></li> <li style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; margin-bottom: 0.5em;"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;">数组的长度指的是数组的元素个数不是数组空间长度。sizeof()关键字即可获取数组总的字节数,在除以元素类型的字节数即可得到数组长度。</span></li> <li style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; margin-bottom: 0.5em;">C语言并不会判断数组访问越界,需要程序员判断越界访问。eg: <code style="font-size: inherit; line-height: inherit; overflow-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0px 2px; color: rgb(233, 105, 0); background: rgb(248, 248, 248);">int arr[5] = {1,2,3,4,5}; int b = arr[10];</code>,这样访问也是可以拿到元素的,天知道你访问的是谁的数据。</li> <li style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; margin-bottom: 0.5em;"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;">数组底层内存结构是连续的。正是由于数组结构的连续性便诞生了内存的友好性,数组分配内存是整块分配的,堆内存很友好;连续的内存是的访问内存效率高。</span></li> <li style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; margin-bottom: 0.5em;"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;">数组大小是固定不变的。需要改变大小就需要新开一块大内存的数组,把之前的元素拷贝过来,释放之前的内存。</span></li> <li style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; margin-bottom: 0.5em;"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;">数据根据下标随机访问的时间复杂度为 O(1)</span></li> <li style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; margin-bottom: 0.5em;"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;">数据的插入和删除很低效:</span></li> </ol> <ul style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; padding-left: 32px; list-style-type: disc;"> <li style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; margin-bottom: 0.5em;"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;">如果删除数组末尾的数据,最好情况时间复杂度为 O(1)</span></li> <li style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; margin-bottom: 0.5em;"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;">如果删除开头的数据,则最坏情况时间复杂度为 O(n)</span></li> <li style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; margin-bottom: 0.5em;"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;">平均情况时间复杂度也为 O(n)。</span></li> </ul> <h3 id="h-5" style="color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px; font-weight: bold; font-size: 1.3em;"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;">数组常见问题</span></h3> <h4 id="h-6" style="color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px; font-weight: bold; font-size: 1.2em;"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;">数组长度是一个非常量。</span></h4> <pre style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;"><code class="c language-c hljs cpp" style="overflow-wrap: break-word; margin: 0px 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; overflow-x: auto; padding: 0.5em; background: rgb(30, 30, 30); color: rgb(220, 220, 220); white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;"><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">1</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(86, 156, 214); word-wrap: inherit !important; word-break: inherit !important;">int</span>&nbsp;b;<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">2</span><span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(78, 201, 176); word-wrap: inherit !important; word-break: inherit !important;">scanf</span>(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(214, 157, 133); word-wrap: inherit !important; word-break: inherit !important;">"%d"</span>,&amp;b);<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">3</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(86, 156, 214); word-wrap: inherit !important; word-break: inherit !important;">int</span>&nbsp;arr[<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); word-wrap: inherit !important; word-break: inherit !important;">3</span>*b];<br></code></pre> <p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;"> 不知道你曾经有没有写过这样的代码,反正我写过。数组的长度和内存是在程序<strong style="font-size: inherit; line-height: inherit; font-weight: bold; overflow-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0px 2px; color: rgb(233, 105, 0); background: rgb(248, 248, 248);">编译时</strong>就已经确定了的。b的值是在<strong style="font-size: inherit; line-height: inherit; font-weight: bold; overflow-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0px 2px; color: rgb(233, 105, 0); background: rgb(248, 248, 248);">运行时</strong>才确定的。有两个新名词,<strong style="font-size: inherit; line-height: inherit; font-weight: bold; overflow-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0px 2px; color: rgb(233, 105, 0); background: rgb(248, 248, 248);">程序编译时&amp;程序运行时</strong>。</p> <h4 id="h-7" style="color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px; font-weight: bold; font-size: 1.2em;"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;">数组越界访问</span></h4> <pre style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;"><code class="c language-c hljs cpp" style="overflow-wrap: break-word; margin: 0px 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; overflow-x: auto; padding: 0.5em; background: rgb(30, 30, 30); color: rgb(220, 220, 220); white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;"><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">1</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(86, 156, 214); word-wrap: inherit !important; word-break: inherit !important;">int</span>&nbsp;arr[<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); word-wrap: inherit !important; word-break: inherit !important;">5</span>]&nbsp;=&nbsp;{<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); word-wrap: inherit !important; word-break: inherit !important;">1</span>,<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); word-wrap: inherit !important; word-break: inherit !important;">2</span>,<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); word-wrap: inherit !important; word-break: inherit !important;">3</span>,<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); word-wrap: inherit !important; word-break: inherit !important;">4</span>,<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); word-wrap: inherit !important; word-break: inherit !important;">5</span>};<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">2</span><span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(78, 201, 176); word-wrap: inherit !important; word-break: inherit !important;">printf</span>(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(214, 157, 133); word-wrap: inherit !important; word-break: inherit !important;">"%d\n"</span>,arr[<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); word-wrap: inherit !important; word-break: inherit !important;">-1</span>]);<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">3</span><span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(78, 201, 176); word-wrap: inherit !important; word-break: inherit !important;">printf</span>(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(214, 157, 133); word-wrap: inherit !important; word-break: inherit !important;">"%d\n"</span>,arr[<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); word-wrap: inherit !important; word-break: inherit !important;">1</span>]);<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">4</span><span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(78, 201, 176); word-wrap: inherit !important; word-break: inherit !important;">printf</span>(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(214, 157, 133); word-wrap: inherit !important; word-break: inherit !important;">"%d\n"</span>,arr[<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); word-wrap: inherit !important; word-break: inherit !important;">4</span>]);<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">5</span><span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(78, 201, 176); word-wrap: inherit !important; word-break: inherit !important;">printf</span>(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(214, 157, 133); word-wrap: inherit !important; word-break: inherit !important;">"%d\n"</span>,arr[<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); word-wrap: inherit !important; word-break: inherit !important;">5</span>]);<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">6</span><span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(78, 201, 176); word-wrap: inherit !important; word-break: inherit !important;">printf</span>(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(214, 157, 133); word-wrap: inherit !important; word-break: inherit !important;">"%d\n"</span>,arr[<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 215, 163); word-wrap: inherit !important; word-break: inherit !important;">6</span>]);<br></code></pre> <p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">肉眼可见的错误,编译器竟然没报错。</p> <figure style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;"><img src="https://tva1.sinaimg.cn/large/0082zybply1gc0w5d75saj31ba0j0gon.jpg" alt="" title="" style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; display: block; margin: 0px auto; max-width: 100%;"><figcaption style="line-height: inherit; margin: 0px; padding: 0px; margin-top: 10px; text-align: center; color: rgb(153, 153, 153); font-size: 0.7em;"></figcaption></figure> <h3 id="h-8" style="color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px; font-weight: bold; font-size: 1.3em;"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;">数组相关笔试题目</span></h3> <ol style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; padding-left: 32px; list-style-type: decimal;"> <li style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; margin-bottom: 0.5em;"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;">给你一个数组,求一个k值,使得前k个数的方差 + 后面n-k个数的方差最小 ,时间复杂度可以到O(n)。</span></li> <li style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; margin-bottom: 0.5em;"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;">给定一个n个整型元素的数组a,其中有一个元素出现次数超过n / 2,求这个元素。</span></li> <li style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; margin-bottom: 0.5em;"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;">给定一个含有n个元素的数组,找出数组中的两个元素X和Y使得abs(x-y)最小。</span></li> <li style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; margin-bottom: 0.5em;"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;">在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。</span></li> <li style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; margin-bottom: 0.5em;"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;">在一个长度为n的数组里的所有数字都在0到n-1的范围内。 数组中某些数字是重复的,但不知道有几个数字是重复的。也不知道每个数字重复几次。请找出数组中任意一个重复的数字。 例如,如果输入长度为7的数组{2,3,1,0,2,5,3},那么对应的输出是第一个重复的数字2。</span></li> <li style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; margin-bottom: 0.5em;">给定一个数组A[0,1,…,n-1],请构建一个数组B[0,1,…,n-1],其中B中的元素B[i]=A[0]<em style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; font-style: italic;">A[1]</em>…<em style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; font-style: italic;">A[i-1]</em>A[i+1]<em style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; font-style: italic;">…</em>A[n-1]。不能使用除法。(注意:规定B[0] = A[1] * A[2] * … * A[n-1],B[n-1] = A[0] * A[1] * … * A[n-2];)</li> </ol> <p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">利用数组可以出很多笔试题目,当然这些题目很多并不是考验数组本身特性大多是考算法基础的。本节就到这里了,有什么不清楚的问题欢迎留言喔,也可私信或mail。</p> <figure style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;"><img src="https://tva1.sinaimg.cn/large/0082zybply1gc0x96ukkrj30730730sn.jpg" alt="" title="" style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; display: block; margin: 0px auto; max-width: 100%;"><figcaption style="line-height: inherit; margin: 0px; padding: 0px; margin-top: 10px; text-align: center; color: rgb(153, 153, 153); font-size: 0.7em;"></figcaption></figure> <h4 style="line-height: inherit; padding: 0px; margin: 1.5em 0px; font-weight: bold; font-size: 1.2em; color: blue; text-align: center;"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;">「转发」是明目张胆的喜欢,「在看」是偷偷摸摸的爱。</span></h4> <p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">往期精彩回顾:</p> <ul style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; padding-left: 32px; list-style-type: disc;"> <li style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; margin-bottom: 0.5em;"><a href="http://mp.weixin.qq.com/s?__biz=MzI5MTMxMDk1Nw==&amp;mid=2247483745&amp;idx=1&amp;sn=0a6864ce6fef4efd16b514a6391c50ae&amp;chksm=ec13dd63db6454757c4563ec214d836bf0258a87bf5be17fdcad7cc29235b402c224c4ed6323&amp;scene=21#wechat_redirect" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; text-decoration: none; color: rgb(30, 107, 184); overflow-wrap: break-word;">初学编程,该如何选择编程语言?</a></li> <li style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; margin-bottom: 0.5em;"><a href="http://mp.weixin.qq.com/s?__biz=MzI5MTMxMDk1Nw==&amp;mid=2247483692&amp;idx=1&amp;sn=8a3174f5ad83ab3ec585f13980f059f7&amp;chksm=ec13dd2edb64543889732d7e3791791b27fc2fd7f96f4f6c85693afa9f419c2a3748bf07de30&amp;scene=21#wechat_redirect" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; text-decoration: none; color: rgb(30, 107, 184); overflow-wrap: break-word;">学习linux命令,看这篇2w多字的命令详解就够了</a></li> <li style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; margin-bottom: 0.5em;"><a href="http://mp.weixin.qq.com/s?__biz=MzI5MTMxMDk1Nw==&amp;mid=2247483777&amp;idx=1&amp;sn=51ab1db4d842edabf2be4dc142530281&amp;chksm=ec13dd83db645495fae7ade69dcb37fc020fd4db5287cef9c27b978e35fe86704e89e2170787&amp;scene=21#wechat_redirect" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; text-decoration: none; color: rgb(30, 107, 184); overflow-wrap: break-word;">带你重新认识指针(上)</a></li> <li style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; margin-bottom: 0.5em;"><a href="http://mp.weixin.qq.com/s?__biz=MzI5MTMxMDk1Nw==&amp;mid=2247483796&amp;idx=1&amp;sn=edbceaf47ee3eb60b197376f6bbd1aea&amp;chksm=ec13dd96db645480d307349adcf8b2a0384f301593a7acd7124bcf3b3c84aa1fa8530181e546&amp;scene=21#wechat_redirect" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; text-decoration: none; color: rgb(30, 107, 184); overflow-wrap: break-word;">指针(下)</a></li> </ul> <figure style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;"><img src="https://tva1.sinaimg.cn/large/0082zybply1gc1gn6x0rkj30p00dwn02.jpg" alt="" title="" style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; display: block; margin: 0px auto; max-width: 100%;"><figcaption style="line-height: inherit; margin: 0px; padding: 0px; margin-top: 10px; text-align: center; color: rgb(153, 153, 153); font-size: 0.7em;"></figcaption></figure></div>

原文出处:https://www.cnblogs.com/zhonglongbo/p/12329865.html

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部