118 lines
4.8 KiB
Markdown
118 lines
4.8 KiB
Markdown
---
|
||
title: ポインタの勉強
|
||
author: kazu634
|
||
date: 2008-07-05
|
||
url: /2008/07/05/_1032/
|
||
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:4117;}s:9:"hash_tags";a:0:{}s:8:"accounts";a:1:{i:0;s:7:"kazu634";}}'
|
||
categories:
|
||
- C
|
||
- Programming
|
||
|
||
---
|
||
<div class="section">
|
||
<h4>
|
||
関数宣言におけるポインタ
|
||
</h4>
|
||
|
||
<p>
|
||
STDINから文字列を読み込んで、それが256文字以内の連続した[A-Za-z0-9]であれば、それを逐一表示する。
|
||
</p>
|
||
|
||
<p>
|
||
以下の二つは同じだよ。
|
||
</p>
|
||
|
||
<pre class="syntax-highlight">
|
||
<span class="synType">int</span> get_word(<span class="synType">char</span> *buf ...)
|
||
<span class="synType">int</span> get_word(<span class="synType">char</span> buf[])
|
||
</pre>
|
||
|
||
<p>
|
||
そこら辺に注意して読んでいこう!
|
||
</p>
|
||
|
||
<pre class="syntax-highlight">
|
||
<span class="synComment">/* Include Files */</span>
|
||
<span class="synPreProc">#include </span><span class="synConstant"><stdio.h></span>
|
||
<span class="synPreProc">#include </span><span class="synConstant"><ctype.h></span>
|
||
<span class="synPreProc">#include </span><span class="synConstant"><stdlib.h></span>
|
||
<span class="synComment">/* ================ */</span>
|
||
<span class="synComment">/* === function === */</span>
|
||
<span class="synComment">/* ================ */</span>
|
||
<span class="synType">int</span> get_word(<span class="synType">char</span> *buf, <span class="synType">int</span> buf_size, <span class="synType">FILE</span> *fp)
|
||
{
|
||
<span class="synType">int</span> len;
|
||
<span class="synType">int</span> ch;
|
||
<span class="synComment">/* Go throgh the spaces of the file */</span>
|
||
<span class="synComment">/* getc --> obtains the next input character (if present)</span>
|
||
<span class="synComment"> from the stream pointed at by </span>
|
||
<span class="synComment"> stream */</span>
|
||
<span class="synComment">/* isalnum --> tests for any character</span>
|
||
<span class="synComment"> for which isalpha(3) or isdigit(3) is true. */</span>
|
||
<span class="synStatement">while</span> ((ch = getc(fp)) != <span class="synConstant">EOF</span> && !isalnum(ch));
|
||
<span class="synComment">/* Here, variable ch contains the first character! */</span>
|
||
len = <span class="synConstant"></span>;
|
||
<span class="synStatement">do</span>{
|
||
<span class="synComment">/* buf[len] = (the pointer of the array buf[0]) + len */</span>
|
||
buf[len] = ch;
|
||
len++;
|
||
<span class="synStatement">if</span> (len >= buf_size){
|
||
fprintf(<span class="synConstant">stderr</span>, <span class="synConstant">"word too long.</span><span class="synSpecial">\n</span><span class="synConstant">"</span>);
|
||
exit(<span class="synConstant">1</span>);
|
||
}
|
||
} <span class="synStatement">while</span> ((ch = getc(fp)) != <span class="synConstant">EOF</span> && isalnum(ch));
|
||
<span class="synComment">/* Add a EOF character at the end. */</span>
|
||
buf[len] = <span class="synSpecial">'\0'</span>;
|
||
<span class="synStatement">return</span> len;
|
||
}
|
||
<span class="synType">int</span> main(<span class="synType">void</span>)
|
||
{
|
||
<span class="synType">char</span> buf[<span class="synConstant">256</span>];
|
||
<span class="synComment">/* stdinとbufは別物だから切り離して考える! */</span>
|
||
<span class="synStatement">while</span> (get_word(buf, <span class="synConstant">256</span>, <span class="synConstant">stdin</span>) != <span class="synConstant">EOF</span>){
|
||
printf(<span class="synConstant">"<<</span><span class="synSpecial">%s</span><span class="synConstant">>></span><span class="synSpecial">\n</span><span class="synConstant">"</span>, buf);
|
||
}
|
||
}
|
||
</pre>
|
||
|
||
<h4>
|
||
関数へのポインタ
|
||
</h4>
|
||
|
||
<blockquote>
|
||
<p>
|
||
たとえば、
|
||
</p>
|
||
|
||
<pre class="syntax-highlight">
|
||
<span class="synType">int</span> func(<span class="synType">double</span> d);
|
||
</pre>
|
||
|
||
<p>
|
||
というプロトタイプの関数があったとき、関数funcへのポインタを格納するポインタ変数は、以下のように宣言します。
|
||
</p>
|
||
|
||
<pre class="syntax-highlight">
|
||
<span class="synType">int</span> (*func_p)(<span class="synType">double</span>);
|
||
</pre>
|
||
|
||
<p>
|
||
そして、以下のように書くと、func_pを経由してfuncを呼び出すことができます。
|
||
</p>
|
||
|
||
<pre class="syntax-highlight">
|
||
<span class="synType">int</span> (*func_p)(<span class="synType">double</span>);
|
||
func_p = func;
|
||
func_p(<span class="synConstant">0.5</span>);
|
||
</pre>
|
||
</blockquote>
|
||
|
||
<p>
|
||
ちなみに関数へのポインタ変数を格納する配列は
|
||
</p>
|
||
|
||
<pre class="syntax-highlight">
|
||
<span class="synType">int</span> (*func_p[])(<span class="synType">double</span>);
|
||
</pre>
|
||
</div> |