首页  >  文章  >  后端开发  >  在C语言中使用指针算术来计算数组的总和

在C语言中使用指针算术来计算数组的总和

WBOY
WBOY转载
2023-09-01 10:53:071018浏览

在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删除