126 lines
7.0 KiB
Markdown
126 lines
7.0 KiB
Markdown
|
---
|
||
|
title: ポインタを使った演習も実践的になってきたよ
|
||
|
author: kazu634
|
||
|
date: 2008-07-11
|
||
|
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:4129;}s:9:"hash_tags";a:0:{}s:8:"accounts";a:1:{i:0;s:7:"kazu634";}}'
|
||
|
categories:
|
||
|
- C
|
||
|
- Programming
|
||
|
|
||
|
---
|
||
|
<div class="section">
|
||
|
<p>
|
||
|
『<a href="http://d.hatena.ne.jp/asin/4774111422" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://d.hatena.ne.jp/asin/4774111422', 'C言語ポインタ完全制覇 (標準プログラマーズライブラリ)');">C言語ポインタ完全制覇 (標準プログラマーズライブラリ)</a>』の勉強だよ。
|
||
|
</p>
|
||
|
|
||
|
<h4>
|
||
|
mallocを用いた可変長配列
|
||
|
</h4>
|
||
|
|
||
|
<pre class="syntax-highlight">
|
||
|
<span class="synComment">/* ======================= */</span>
|
||
|
<span class="synComment">/* === Include library === */</span>
|
||
|
<span class="synComment">/* ======================= */</span>
|
||
|
<span class="synPreProc">#include </span><span class="synConstant"><stdio.h></span>
|
||
|
<span class="synPreProc">#include </span><span class="synConstant"><stdlib.h></span>
|
||
|
<span class="synComment">/* ================= */</span>
|
||
|
<span class="synComment">/* === Functions === */</span>
|
||
|
<span class="synComment">/* ================= */</span>
|
||
|
<span class="synType">int</span> main(<span class="synType">void</span>)
|
||
|
{
|
||
|
<span class="synType">char</span> buf[<span class="synConstant">256</span>];
|
||
|
<span class="synType">int</span> size;
|
||
|
<span class="synType">int</span> *variable_array;
|
||
|
<span class="synType">int</span> i;
|
||
|
printf(<span class="synConstant">"Input Array Size: "</span>);
|
||
|
<span class="synComment">/* Read Data from STDIN */</span>
|
||
|
fgets(buf, <span class="synConstant">256</span>, <span class="synConstant">stdin</span>);
|
||
|
sscanf(buf, <span class="synConstant">"</span><span class="synSpecial">%d</span><span class="synConstant">"</span>, &size);
|
||
|
variable_array = malloc(<span class="synStatement">sizeof</span>(<span class="synType">int</span>) * size);
|
||
|
<span class="synStatement">for</span> (i = <span class="synConstant"></span>; i < size; ++i)
|
||
|
{
|
||
|
variable_array[i] = i;
|
||
|
}
|
||
|
<span class="synStatement">for</span> (i = <span class="synConstant"></span>;i < size; ++i)
|
||
|
{
|
||
|
printf(<span class="synConstant">"variable_array[</span><span class="synSpecial">%d</span><span class="synConstant">] : </span><span class="synSpecial">%d\n</span><span class="synConstant">"</span>, i, variable_array[i]);
|
||
|
}
|
||
|
<span class="synStatement">return</span> <span class="synConstant"></span>;
|
||
|
}
|
||
|
</pre>
|
||
|
|
||
|
<h4>
|
||
|
可変長配列の配列の一つの例
|
||
|
</h4>
|
||
|
|
||
|
<pre class="syntax-highlight">
|
||
|
<span class="synComment">/* ======================= */</span>
|
||
|
<span class="synComment">/* === Include library === */</span>
|
||
|
<span class="synComment">/* ======================= */</span>
|
||
|
<span class="synPreProc">#include </span><span class="synConstant"><stdio.h></span>
|
||
|
<span class="synPreProc">#include </span><span class="synConstant"><stdlib.h></span>
|
||
|
<span class="synPreProc">#include </span><span class="synConstant"><string.h></span>
|
||
|
<span class="synComment">/* ================= */</span>
|
||
|
<span class="synComment">/* === Functions === */</span>
|
||
|
<span class="synComment">/* ================= */</span>
|
||
|
<span class="synType">void</span> read_slogan(<span class="synType">FILE</span> *fp, <span class="synType">char</span> **slogan)
|
||
|
{
|
||
|
<span class="synType">char</span> buf[<span class="synConstant">1024</span>];
|
||
|
<span class="synType">int</span> i;
|
||
|
<span class="synStatement">for</span> (i = <span class="synConstant"></span>; i < <span class="synConstant">7</span>; i++)
|
||
|
{
|
||
|
fgets(buf, <span class="synConstant">1024</span>, fp);
|
||
|
buf[strlen(buf) - <span class="synConstant">1</span>] = <span class="synSpecial">'\0'</span>;
|
||
|
slogan[i] = malloc(<span class="synStatement">sizeof</span>(<span class="synType">char</span>) * (strlen(buf) + <span class="synConstant">1</span>));
|
||
|
strcpy(slogan[i], buf);
|
||
|
}
|
||
|
}
|
||
|
<span class="synType">int</span> main(<span class="synType">void</span>)
|
||
|
{
|
||
|
<span class="synType">char</span> *slogan[<span class="synConstant">7</span>];
|
||
|
<span class="synType">int</span> i;
|
||
|
read_slogan(<span class="synConstant">stdin</span>, slogan);
|
||
|
<span class="synStatement">for</span> (i = <span class="synConstant"></span>; i < <span class="synConstant">7</span>; ++i)
|
||
|
{
|
||
|
printf(<span class="synConstant">"</span><span class="synSpecial">%s\n</span><span class="synConstant">"</span>, slogan[i]);
|
||
|
}
|
||
|
<span class="synStatement">return</span> <span class="synConstant"></span>;
|
||
|
}
|
||
|
</pre>
|
||
|
|
||
|
<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>
|