190 lines
7.5 KiB
Markdown
190 lines
7.5 KiB
Markdown
|
---
|
||
|
title: 『C言語ポインタ完全制覇 (標準プログラマーズライブラリ)』
|
||
|
author: kazu634
|
||
|
date: 2008-07-04
|
||
|
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:4113;}s:9:"hash_tags";a:0:{}s:8:"accounts";a:1:{i:0;s:7:"kazu634";}}'
|
||
|
categories:
|
||
|
- C
|
||
|
- Programming
|
||
|
|
||
|
---
|
||
|
<div class="section">
|
||
|
<blockquote>
|
||
|
<p>
|
||
|
Cは、ちょうど、このダンの工具のような言語です。つまり、
|
||
|
</p>
|
||
|
|
||
|
<ul>
|
||
|
<li>
|
||
|
現場の人間が、目の前の問題を解決するために作成した言語であり、
|
||
|
</li>
|
||
|
<li>
|
||
|
非常に便利ではあるけれども、
|
||
|
</li>
|
||
|
<li>
|
||
|
見た目あちこち不格好で
|
||
|
</li>
|
||
|
<li>
|
||
|
よくわかっていない人がうっかり使うと危険な目にあったりする
|
||
|
</li>
|
||
|
</ul>
|
||
|
|
||
|
<p>
|
||
|
言語なのです。
|
||
|
</p>
|
||
|
</blockquote>
|
||
|
|
||
|
<h4>
|
||
|
Spirit of C
|
||
|
</h4>
|
||
|
|
||
|
<blockquote>
|
||
|
<ol>
|
||
|
<li>
|
||
|
Trust the programmer.
|
||
|
</li>
|
||
|
<li>
|
||
|
Don’t prevent the programmer from doing what needs to be done.
|
||
|
</li>
|
||
|
<li>
|
||
|
Keep the language small and simple.
|
||
|
</li>
|
||
|
<li>
|
||
|
Provide only one way to do an operation.
|
||
|
</li>
|
||
|
<li>
|
||
|
Make it fast, even if it is not guranteed to be portable.
|
||
|
</li>
|
||
|
</ol>
|
||
|
</blockquote>
|
||
|
|
||
|
<p>
|
||
|
<a name="seemore"></a>
|
||
|
</p>
|
||
|
|
||
|
<h4>
|
||
|
ポインタの練習だよ
|
||
|
</h4>
|
||
|
|
||
|
<pre class="syntax-highlight">
|
||
|
<span class="synPreProc">#include </span><span class="synConstant"><stdio.h></span>
|
||
|
<span class="synType">int</span> main(<span class="synType">void</span>)
|
||
|
{
|
||
|
<span class="synType">int</span> hoge = <span class="synConstant">5</span>;
|
||
|
<span class="synType">int</span> piyo = <span class="synConstant">10</span>;
|
||
|
<span class="synType">int</span> *hoge_p;
|
||
|
<span class="synComment">/* show the addresses of each variables */</span>
|
||
|
printf(<span class="synConstant">"&hoge...</span><span class="synSpecial">%p\n</span><span class="synConstant">"</span>, &hoge);
|
||
|
printf(<span class="synConstant">"&piyo...</span><span class="synSpecial">%p\n</span><span class="synConstant">"</span>, &piyo);
|
||
|
printf(<span class="synConstant">"&hoge_p...</span><span class="synSpecial">%p\n</span><span class="synConstant">"</span>, &hoge_p);
|
||
|
hoge_p =
|
||
|
printf(<span class="synConstant">"*hoge_p...</span><span class="synSpecial">%d\n</span><span class="synConstant">"</span>, *hoge_p);
|
||
|
*hoge_p = <span class="synConstant">10</span>;
|
||
|
printf(<span class="synConstant">"hoge...</span><span class="synSpecial">%d\n</span><span class="synConstant">"</span>, hoge);
|
||
|
<span class="synStatement">return</span>(<span class="synConstant"></span>);
|
||
|
}
|
||
|
</pre>
|
||
|
|
||
|
<h4>
|
||
|
なんでも指せるポインタ型 void*
|
||
|
</h4>
|
||
|
|
||
|
<pre class="syntax-highlight">
|
||
|
<span class="synPreProc">#include </span><span class="synConstant"><stdio.h></span>
|
||
|
<span class="synType">int</span> main(<span class="synType">void</span>)
|
||
|
{
|
||
|
<span class="synType">int</span> hoge = <span class="synConstant">5</span>;
|
||
|
<span class="synType">void</span> *hoge_p;
|
||
|
hoge_p =
|
||
|
printf(<span class="synConstant">"</span><span class="synSpecial">%d\n</span><span class="synConstant">"</span>, *(<span class="synType">int</span>*)hoge_p);
|
||
|
<span class="synStatement">return</span>(<span class="synConstant"></span>);
|
||
|
}
|
||
|
</pre>
|
||
|
|
||
|
<p>
|
||
|
型キャストするのがポイントらしい。
|
||
|
</p>
|
||
|
|
||
|
<h4>
|
||
|
ポインタで配列の走査
|
||
|
</h4>
|
||
|
|
||
|
<pre class="syntax-highlight">
|
||
|
<span class="synPreProc">#include </span><span class="synConstant"><stdio.h></span>
|
||
|
<span class="synType">int</span> main(<span class="synType">void</span>)
|
||
|
{
|
||
|
<span class="synType">int</span> array[<span class="synConstant">5</span>];
|
||
|
<span class="synType">int</span> *p;
|
||
|
<span class="synType">int</span> i;
|
||
|
<span class="synStatement">for</span> (i = <span class="synConstant"></span>; i < <span class="synConstant">5</span>; i++)
|
||
|
{
|
||
|
array[i] = i;
|
||
|
}
|
||
|
<span class="synStatement">for</span> (p = array; p != &array[<span class="synConstant">5</span>]; ++p)
|
||
|
{
|
||
|
printf(<span class="synConstant">"</span><span class="synSpecial">%d\n</span><span class="synConstant">"</span>, *p);
|
||
|
}
|
||
|
<span class="synStatement">return</span>(<span class="synConstant"></span>);
|
||
|
}
|
||
|
</pre>
|
||
|
|
||
|
<p>
|
||
|
こんなポインタの使い方がありな訳ですね。そしてこれもあり。
|
||
|
</p>
|
||
|
|
||
|
<pre class="syntax-highlight">
|
||
|
<span class="synPreProc">#include </span><span class="synConstant"><stdio.h></span>
|
||
|
<span class="synType">int</span> main(<span class="synType">void</span>)
|
||
|
{
|
||
|
<span class="synType">int</span> array[<span class="synConstant">5</span>];
|
||
|
<span class="synType">int</span> *p;
|
||
|
<span class="synType">int</span> i;
|
||
|
<span class="synStatement">for</span> (i = <span class="synConstant"></span>; i < <span class="synConstant">5</span>; i++)
|
||
|
{
|
||
|
array[i] = i;
|
||
|
}
|
||
|
p = array;
|
||
|
<span class="synStatement">for</span> (i = <span class="synConstant"></span>; i < <span class="synConstant">5</span>; i++)
|
||
|
{
|
||
|
printf(<span class="synConstant">"</span><span class="synSpecial">%d\n</span><span class="synConstant">"</span>, *(p + i));
|
||
|
}
|
||
|
<span class="synStatement">return</span>(<span class="synConstant"></span>);
|
||
|
}
|
||
|
</pre></p>
|
||
|
|
||
|
<div class="hatena-asin-detail">
|
||
|
<a href="http://www.amazon.co.jp/dp/4774111422/?tag=hatena_st1-22&ascsubtag=d-7ibv" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://www.amazon.co.jp/dp/4774111422/?tag=hatena_st1-22&ascsubtag=d-7ibv', '');"><img src="https://images-na.ssl-images-amazon.com/images/I/51FT9JJP2VL._SL160_.jpg" class="hatena-asin-detail-image" alt="C言語ポインタ完全制覇 (標準プログラマーズライブラリ)" title="C言語ポインタ完全制覇 (標準プログラマーズライブラリ)" /></a></p>
|
||
|
|
||
|
<div class="hatena-asin-detail-info">
|
||
|
<p class="hatena-asin-detail-title">
|
||
|
<a href="http://www.amazon.co.jp/dp/4774111422/?tag=hatena_st1-22&ascsubtag=d-7ibv" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://www.amazon.co.jp/dp/4774111422/?tag=hatena_st1-22&ascsubtag=d-7ibv', 'C言語ポインタ完全制覇 (標準プログラマーズライブラリ)');">C言語ポインタ完全制覇 (標準プログラマーズライブラリ)</a>
|
||
|
</p>
|
||
|
|
||
|
<ul>
|
||
|
<li>
|
||
|
<span class="hatena-asin-detail-label">作者:</span> <a href="http://d.hatena.ne.jp/keyword/%C1%B0%B6%B6%CF%C2%CC%EF" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://d.hatena.ne.jp/keyword/%C1%B0%B6%B6%CF%C2%CC%EF', '前橋和弥');" class="keyword">前橋和弥</a>
|
||
|
</li>
|
||
|
<li>
|
||
|
<span class="hatena-asin-detail-label">出版社/メーカー:</span> <a href="http://d.hatena.ne.jp/keyword/%B5%BB%BD%D1%C9%BE%CF%C0%BC%D2" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://d.hatena.ne.jp/keyword/%B5%BB%BD%D1%C9%BE%CF%C0%BC%D2', '技術評論社');" class="keyword">技術評論社</a>
|
||
|
</li>
|
||
|
<li>
|
||
|
<span class="hatena-asin-detail-label">発売日:</span> 2001/01
|
||
|
</li>
|
||
|
<li>
|
||
|
<span class="hatena-asin-detail-label">メディア:</span> 単行本
|
||
|
</li>
|
||
|
<li>
|
||
|
<span class="hatena-asin-detail-label">購入</span>: 22人 <span class="hatena-asin-detail-label">クリック</span>: 147回
|
||
|
</li>
|
||
|
<li>
|
||
|
<a href="http://d.hatena.ne.jp/asin/4774111422" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://d.hatena.ne.jp/asin/4774111422', 'この商品を含むブログ (75件) を見る');" target="_blank">この商品を含むブログ (75件) を見る</a>
|
||
|
</li>
|
||
|
</ul>
|
||
|
</div>
|
||
|
|
||
|
<div class="hatena-asin-detail-foot">
|
||
|
</div>
|
||
|
</div>
|
||
|
</div>
|