void s(int* a, int len) {int"/> void s(int* a, int len) {int">

Home  >  Article  >  Backend Development  >  Using pointer arithmetic to calculate sum of array in C

Using pointer arithmetic to calculate sum of array in C

WBOY
WBOYforward
2023-09-01 10:53:071020browse

Using pointer arithmetic to calculate sum of array in C

In this program, we need to use pointer arithmetic to find the sum of array elements.

Here we use * to represent the value stored at the memory address, and the address will remain stored in the variable. So "int *ptr" means that ptr is a variable containing an address whose content is an integer.

*p means it is a pointer variable. Using this and sum() we will find the sum of the array elements.

Sample code

<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>

Output

Sum of array = 12

Algorithm

Begin
   Initialize array to hold the variables.
   Call function s to get the sum of the variables.
   Print the sum.
End.

The above is the detailed content of Using pointer arithmetic to calculate sum of array in C. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete
Previous article:Arrays in C/C++ programsNext article:Arrays in C/C++ programs