今天看到一道题目,研究了好一会儿终于高明白了,在这里拿出来和大家分享一下:
<code class="hljs perl has-numbering">大家可以先行计算一下。 <span class="hljs-comment">#include <stdio.h></span> char <span class="hljs-variable">*c</span>[] = { <span class="hljs-string">"ENTER"</span>, <span class="hljs-string">"NEW"</span>, <span class="hljs-string">"POINT"</span>, <span class="hljs-string">"FIRST"</span> }; char <span class="hljs-variable">**</span>cp[] = { c + <span class="hljs-number">3</span>, c + <span class="hljs-number">2</span>, c + <span class="hljs-number">1</span>, c }; char <span class="hljs-variable">**</span><span class="hljs-variable">*cpp</span> = cp; <span class="hljs-keyword">int</span> main(void) { <span class="hljs-keyword">printf</span>(<span class="hljs-string">"<span class="hljs-variable">%s</span> "</span>, <span class="hljs-variable">**</span>++cpp); <span class="hljs-keyword">printf</span>(<span class="hljs-string">"<span class="hljs-variable">%s</span> "</span>, <span class="hljs-variable">*-</span>-<span class="hljs-variable">*+</span>+cpp + <span class="hljs-number">3</span>); <span class="hljs-keyword">printf</span>(<span class="hljs-string">"<span class="hljs-variable">%s</span> "</span>, <span class="hljs-variable">*cpp</span>[-<span class="hljs-number">2</span>] + <span class="hljs-number">3</span>); <span class="hljs-keyword">printf</span>(<span class="hljs-string">"<span class="hljs-variable">%s</span>\n"</span>, cpp[-<span class="hljs-number">1</span>][-<span class="hljs-number">1</span>] + <span class="hljs-number">1</span>); <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>; } </code><ul class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>12</li><li>13</li><li>14</li><li>15</li><li>16</li></ul><div class="save_code tracking-ad" style="display: none;" data-mod="popu_249"><a target=_blank target="_blank"><img src="http://static.blog.csdn.net/images/save_snippets.png" alt="" /></a></div>
那么究竟输出的结果是什么呢?
POINT ER ST EW
不知道小伙伴们有么有做对
接下来就是对答案的分析:
<code class="hljs perl has-numbering">(<span class="hljs-number">1</span>) <span class="hljs-keyword">printf</span>(<span class="hljs-string">"<span class="hljs-variable">%s</span> "</span>, <span class="hljs-variable">**</span>++cpp);<span class="hljs-regexp">//</span><span class="hljs-string">"POINT"</span> cpp=cp cpp的值就是数组cp的首地址,即存储c+<span class="hljs-number">3</span>这块空间的地址。 ++cpp 即cpp自加一,cpp的值为存储c+<span class="hljs-number">2</span>这块空间的地址 <span class="hljs-variable">**</span>++cpp 即<span class="hljs-variable">*(</span>c+<span class="hljs-number">2</span>),即POINT的首地址 (<span class="hljs-number">2</span>) <span class="hljs-keyword">printf</span>(<span class="hljs-string">"<span class="hljs-variable">%s</span> "</span>, <span class="hljs-variable">*-</span>-<span class="hljs-variable">*+</span>+cpp + <span class="hljs-number">3</span>);<span class="hljs-regexp">//</span>ER ++cpp cpp的值为存储c+<span class="hljs-number">1</span>这块空间的地址 <span class="hljs-variable">*+</span>+cpp 拿到的就是数组cp的成员c+<span class="hljs-number">1</span> --<span class="hljs-variable">*+</span>+cpp 即--(c+<span class="hljs-number">1</span>),数组cp的第三个成员变为c <span class="hljs-variable">*-</span>-<span class="hljs-variable">*+</span>+cpp+<span class="hljs-number">3</span> 即<span class="hljs-variable">*c</span>+<span class="hljs-number">3</span>,就是ER的首地址 (<span class="hljs-number">3</span>) <span class="hljs-keyword">printf</span>(<span class="hljs-string">"<span class="hljs-variable">%s</span> "</span>, <span class="hljs-variable">*cpp</span>[-<span class="hljs-number">2</span>] + <span class="hljs-number">3</span>);<span class="hljs-regexp">//</span>ST cpp[-<span class="hljs-number">2</span>] 即<span class="hljs-variable">*(</span>cpp-<span class="hljs-number">2</span>),cpp的值为存储c+<span class="hljs-number">1</span>这块空间的地址(这块空间里的内容被改成了c),结果就是拿到c+<span class="hljs-number">3</span> <span class="hljs-variable">*cpp</span>[-<span class="hljs-number">2</span>] + <span class="hljs-number">3</span> 即ST的首地址 (<span class="hljs-number">4</span>)<span class="hljs-keyword">printf</span>(<span class="hljs-string">"<span class="hljs-variable">%s</span>\n"</span>, cpp[-<span class="hljs-number">1</span>][-<span class="hljs-number">1</span>] + <span class="hljs-number">1</span>);<span class="hljs-regexp">//</span>EW cpp[-<span class="hljs-number">1</span>] 即<span class="hljs-variable">*(</span>cpp-<span class="hljs-number">1</span>),cpp的值为存储c+<span class="hljs-number">1</span>这块空间的地址(这块空间里的内容被改成了c),结果就是拿到c+<span class="hljs-number">2</span> cpp[-<span class="hljs-number">1</span>][-<span class="hljs-number">1</span>] 即<span class="hljs-variable">*(</span>(c+<span class="hljs-number">2</span>)-<span class="hljs-number">1</span>),拿到NEW的首地址 </code>