Heim > Artikel > Backend-Entwicklung > Verwenden der Zeigerarithmetik zur Berechnung der Array-Summe in C
In diesem Programm müssen wir Zeigerarithmetik verwenden, um die Summe der Array-Elemente zu ermitteln.
Hier verwenden wir *, um den an der Speicheradresse gespeicherten Wert darzustellen, und die Adresse bleibt in der Variablen gespeichert. Daher bedeutet „int *ptr“, dass ptr eine Variable ist, die eine Adresse enthält, deren Inhalt eine Ganzzahl ist.
*p bedeutet, dass es sich um eine Zeigervariable handelt. Mit this und sum() ermitteln wir die Summe der Array-Elemente.
<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.
Das obige ist der detaillierte Inhalt vonVerwenden der Zeigerarithmetik zur Berechnung der Array-Summe in C. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!