このプログラムでは、ポインター演算を使用して配列要素の合計を求める必要があります。
ここでは、メモリ アドレスに格納される値を表すために * を使用します。アドレスは変数に格納されたままになります。したがって、「int *ptr」は、ptr が整数の内容を持つアドレスを含む変数であることを意味します。
*p はポインタ変数であることを意味します。これと sum() を使用して、配列要素の合計を求めます。
<span class="com">#include</span><span class="pln"> </span><span class="str"><stdio.h></span> <span class="kwd">void</span><span class="pln"> s</span><span class="pun">(</span><span class="kwd">int</span><span class="pun">*</span><span class="pln"> a</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> len</span><span class="pun">) </span><span class="pun">{</span> <span class="kwd"> int</span><span class="pln"> i</span><span class="pun">,</span><span class="pln"> s_of_arr </span><span class="pun">=</span><span class="pln"> </span><span class="lit">0</span><span class="pun">;</span> <span class="kwd"> for</span><span class="pln"> </span><span class="pun">(</span><span class="pln">i </span><span class="pun">=</span><span class="pln"> </span><span class="lit">0</span><span class="pun">;</span><span class="pln"> i </span><span class="pun"><</span><span class="pln"> len</span><span class="pun">;</span><span class="pln"> i</span><span class="pun">++)</span> <span class="pln"> s_of_arr </span><span class="pun">=</span><span class="pln"> s_of_arr </span><span class="pun">+</span><span class="pln"> </span><span class="pun">*(</span><span class="pln">a </span><span class="pun">+</span><span class="pln"> i</span><span class="pun">);</span> <span class="pln"> printf</span><span class="pun">(</span><span class="pln"> </span><span class="str">"sum of array is = %d"</span><span class="pln"> </span><span class="pun">,</span><span class="pln">s_of_arr</span><span class="pun">);</span> <span class="pun">}</span> <span class="kwd">int</span><span class="pln"> main</span><span class="pun">() </span><span class="pun">{</span> <span class="kwd"> int</span><span class="pln"> arr</span><span class="pun">[]</span><span class="pln"> </span><span class="pun">=</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="lit">1</span><span class="pun">,</span><span class="lit">2</span><span class="pun">,</span><span class="lit">4</span><span class="pun">,</span><span class="lit">6</span><span class="pun">,</span><span class="lit">7</span><span class="pun">,-</span><span class="lit">5</span><span class="pun">,-</span><span class="lit">3</span><span class="pln"> </span><span class="pun">};</span> <span class="pln"> s</span><span class="pun">(</span><span class="pln">arr</span><span class="pun">,</span><span class="pln"> </span><span class="lit">7</span><span class="pun">);</span> <span class="kwd"> return</span><span class="pln"> </span><span class="lit">0</span><span class="pun">;</span> <span class="pun">}</span>
Sum of array = 12
Begin Initialize array to hold the variables. Call function s to get the sum of the variables. Print the sum. End.
以上がC でポインター演算を使用して配列の合計を計算するの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。