105 lines
6.9 KiB
Markdown
105 lines
6.9 KiB
Markdown
---
|
|
title: ポインタを使った配列走査
|
|
author: kazu634
|
|
date: 2008-06-20
|
|
url: /2008/06/20/_1023/
|
|
wordtwit_post_info:
|
|
- 'O:8:"stdClass":13:{s:6:"manual";b:0;s:11:"tweet_times";i:1;s:5:"delay";i:0;s:7:"enabled";i:1;s:10:"separation";s:2:"60";s:7:"version";s:3:"3.7";s:14:"tweet_template";b:0;s:6:"status";i:2;s:6:"result";a:0:{}s:13:"tweet_counter";i:2;s:13:"tweet_log_ids";a:1:{i:0;i:4087;}s:9:"hash_tags";a:0:{}s:8:"accounts";a:1:{i:0;s:7:"kazu634";}}'
|
|
categories:
|
|
- C
|
|
- Programming
|
|
|
|
---
|
|
<div class="section">
|
|
<p>
|
|
以下のプログラムは同じことをしている。
|
|
</p>
|
|
|
|
<pre class="syntax-highlight">
|
|
<span class="synPreProc">#include </span><span class="synConstant"><stdio.h></span>
|
|
<span class="synType">int</span> array[] = {<span class="synConstant">4</span>, <span class="synConstant">5</span>, <span class="synConstant">8</span>, <span class="synConstant">9</span>, <span class="synConstant">8</span>, <span class="synConstant">1</span>, <span class="synConstant"></span>, <span class="synConstant">1</span>, <span class="synConstant">9</span>, <span class="synConstant">3</span>};
|
|
<span class="synType">void</span> show(<span class="synType">int</span> array[], <span class="synType">int</span> num){
|
|
<span class="synType">int</span> i;
|
|
<span class="synStatement">for</span> (i = <span class="synConstant"></span>;i < num;i++) {
|
|
printf (<span class="synConstant">"</span><span class="synSpecial">%d</span><span class="synConstant"> --> </span><span class="synSpecial">%d\n</span><span class="synConstant">"</span>, i, array[i]);
|
|
}
|
|
printf (<span class="synConstant">"</span><span class="synSpecial">\n</span><span class="synConstant">"</span>);
|
|
}
|
|
<span class="synType">int</span> main(){
|
|
<span class="synType">int</span> index = <span class="synConstant"></span>;
|
|
<span class="synType">int</span> num = <span class="synStatement">sizeof</span>(array) / <span class="synStatement">sizeof</span>(array[<span class="synConstant"></span>]);
|
|
show(array, num);
|
|
<span class="synStatement">while</span> (array[index] != <span class="synConstant"></span>) {
|
|
index++;
|
|
}
|
|
printf (<span class="synConstant">"Number of elements before zero </span><span class="synSpecial">%d\n</span><span class="synConstant">"</span>, index);
|
|
<span class="synStatement">return</span>(<span class="synConstant"></span>);
|
|
}
|
|
</pre>
|
|
|
|
<pre class="syntax-highlight">
|
|
<span class="synPreProc">#include </span><span class="synConstant"><stdio.h></span>
|
|
<span class="synType">int</span> array[] = {<span class="synConstant">4</span>, <span class="synConstant">5</span>, <span class="synConstant">8</span>, <span class="synConstant">9</span>, <span class="synConstant">8</span>, <span class="synConstant">1</span>, <span class="synConstant"></span>, <span class="synConstant">1</span>, <span class="synConstant">9</span>, <span class="synConstant">3</span>};
|
|
<span class="synType">void</span> show(<span class="synType">int</span> array[], <span class="synType">int</span> num){
|
|
<span class="synType">int</span> i;
|
|
<span class="synStatement">for</span> (i = <span class="synConstant"></span>;i < num;i++) {
|
|
printf (<span class="synConstant">"</span><span class="synSpecial">%d</span><span class="synConstant"> --> </span><span class="synSpecial">%d\n</span><span class="synConstant">"</span>, i, array[i]);
|
|
}
|
|
printf (<span class="synConstant">"</span><span class="synSpecial">\n</span><span class="synConstant">"</span>);
|
|
}
|
|
<span class="synType">int</span> main(){
|
|
<span class="synType">int</span> *array_ptr = array;
|
|
<span class="synType">int</span> num = <span class="synStatement">sizeof</span>(array) / <span class="synStatement">sizeof</span>(array[<span class="synConstant"></span>]);
|
|
show(array, num);
|
|
<span class="synStatement">while</span> ((*array_ptr) != <span class="synConstant"></span>) {
|
|
array_ptr++;
|
|
}
|
|
printf (<span class="synConstant">"Number of elements before zero </span><span class="synSpecial">%d\n</span><span class="synConstant">"</span>, array_ptr - array);
|
|
<span class="synStatement">return</span>(<span class="synConstant"></span>);
|
|
}
|
|
</pre>
|
|
|
|
<p>
|
|
ここで、「array_ptr – array」は「現在の添え字番号 – 0」という意味になっている。
|
|
</p>
|
|
|
|
<p>
|
|
<center>
|
|
</center>
|
|
</p>
|
|
|
|
<div class="hatena-asin-detail">
|
|
<a href="http://www.amazon.co.jp/dp/4900900648/?tag=hatena_st1-22&ascsubtag=d-7ibv" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://www.amazon.co.jp/dp/4900900648/?tag=hatena_st1-22&ascsubtag=d-7ibv', '');"><img src="https://images-na.ssl-images-amazon.com/images/I/51PWS7A72YL._SL160_.jpg" class="hatena-asin-detail-image" alt="C実践プログラミング 第3版" title="C実践プログラミング 第3版" /></a></p>
|
|
|
|
<div class="hatena-asin-detail-info">
|
|
<p class="hatena-asin-detail-title">
|
|
<a href="http://www.amazon.co.jp/dp/4900900648/?tag=hatena_st1-22&ascsubtag=d-7ibv" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://www.amazon.co.jp/dp/4900900648/?tag=hatena_st1-22&ascsubtag=d-7ibv', 'C実践プログラミング 第3版');">C実践プログラミング 第3版</a>
|
|
</p>
|
|
|
|
<ul>
|
|
<li>
|
|
<span class="hatena-asin-detail-label">作者:</span> <a href="http://d.hatena.ne.jp/keyword/Steve%20Oualline" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://d.hatena.ne.jp/keyword/Steve%20Oualline', 'Steve Oualline');" class="keyword">Steve Oualline</a>,<a href="http://d.hatena.ne.jp/keyword/%CB%BE%B7%EE%B9%AF%BB%CA%28%B4%C6%CC%F5%29" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://d.hatena.ne.jp/keyword/%CB%BE%B7%EE%B9%AF%BB%CA%28%B4%C6%CC%F5%29', '望月康司(監訳)');" class="keyword">望月康司(監訳)</a>,<a href="http://d.hatena.ne.jp/keyword/%C3%AB%B8%FD%B8%F9" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://d.hatena.ne.jp/keyword/%C3%AB%B8%FD%B8%F9', '谷口功');" class="keyword">谷口功</a>
|
|
</li>
|
|
<li>
|
|
<span class="hatena-asin-detail-label">出版社/メーカー:</span> <a href="http://d.hatena.ne.jp/keyword/%A5%AA%A5%E9%A5%A4%A5%EA%A1%BC%A1%A6%A5%B8%A5%E3%A5%D1%A5%F3" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://d.hatena.ne.jp/keyword/%A5%AA%A5%E9%A5%A4%A5%EA%A1%BC%A1%A6%A5%B8%A5%E3%A5%D1%A5%F3', 'オライリー・ジャパン');" class="keyword">オライリー・ジャパン</a>
|
|
</li>
|
|
<li>
|
|
<span class="hatena-asin-detail-label">発売日:</span> 1998/06/15
|
|
</li>
|
|
<li>
|
|
<span class="hatena-asin-detail-label">メディア:</span> 大型本
|
|
</li>
|
|
<li>
|
|
<span class="hatena-asin-detail-label">購入</span>: 7人 <span class="hatena-asin-detail-label">クリック</span>: 158回
|
|
</li>
|
|
<li>
|
|
<a href="http://d.hatena.ne.jp/asin/4900900648" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://d.hatena.ne.jp/asin/4900900648', 'この商品を含むブログ (45件) を見る');" target="_blank">この商品を含むブログ (45件) を見る</a>
|
|
</li>
|
|
</ul>
|
|
</div>
|
|
|
|
<div class="hatena-asin-detail-foot">
|
|
</div>
|
|
</div>
|
|
</div>
|