首頁 > 後端開發 > C++ > 主體

在C語言中使用指標算術來計算陣列的總和

WBOY
發布: 2023-09-01 10:53:07
轉載
1028 人瀏覽過

在C語言中使用指標算術來計算陣列的總和

在這個程式中,我們需要使用指標運算來求陣列元素的和。

這裡我們使用 * 來表示儲存在記憶體位址處的值,而該位址將保留儲存在變數中。因此「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中文網其他相關文章!

相關標籤:
來源:tutorialspoint.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板