Maison > Article > développement back-end > Utiliser l'arithmétique du pointeur pour calculer la somme d'un tableau en C
Dans ce programme, nous devons utiliser l'arithmétique du pointeur pour trouver la somme des éléments du tableau.
Ici, nous utilisons * pour représenter la valeur stockée à l'adresse mémoire et l'adresse restera stockée dans la variable. Donc « int *ptr » signifie que ptr est une variable contenant une adresse dont le contenu est un entier.
*p signifie qu'il s'agit d'une variable de pointeur. En utilisant ceci et sum(), nous trouverons la somme des éléments du tableau.
<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.
Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!