void s(int* a, int len) {int"/> void s(int* a, int len) {int">
Rumah > Artikel > pembangunan bahagian belakang > Menggunakan aritmetik penunjuk untuk mengira jumlah tatasusunan dalam C
Dalam atur cara ini, kita perlu menggunakan aritmetik penunjuk untuk mencari jumlah elemen tatasusunan.
Di sini kami menggunakan * untuk mewakili nilai yang disimpan pada alamat memori dan alamat akan kekal disimpan dalam pembolehubah. Jadi "int *ptr" bermaksud ptr ialah pembolehubah yang mengandungi alamat yang kandungannya ialah integer.
*p bermaksud ia adalah pembolehubah penunjuk. Menggunakan ini dan sum() kita akan mencari jumlah elemen tatasusunan.
<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.
Atas ialah kandungan terperinci Menggunakan aritmetik penunjuk untuk mengira jumlah tatasusunan dalam C. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!