• 検索結果がありません。

郢晄亢縺ケ晢スウ郢ァソ邵コィ隴ュ諤

N/A
N/A
Protected

Academic year: 2021

シェア "郢晄亢縺ケ晢スウ郢ァソ邵コィ隴ュ諤"

Copied!
3
0
0

読み込み中.... (全文を見る)

全文

(1)

1

ポインタとは

学習のポイント ポインタとは何か、ポインタ用いたデータ参照とは何かを学びます。

1.1

変数の論理イメージと物理イメージ

私たちは変数を介してデータを扱っています。たとえば、 int a, b; a = 10; b = 20; と宣言された変数a、bについて考えてみましょう。  理論的レベルで見ると変数aに10、bに20が格納されているのですが、物理レベルで 見ると変数a、bが割り当てられているメモリにデータ10、20が格納されているのです。  ポインタは、この変数が割り当てられているメモリ上のアドレス(番地)を元に、そこ のデータを取り消したり格納したりすることができます。これをデータを参照するといい ます。

2

ポインタと文字列

学習のポイント ポインタを用いて文字列を操作する方法を学びます。  Cの文字列は””を用いて表します。たとえば”Hello”という文字列はメモリ上に、次の ように格納されています。 H e l l o \0 \0は文字列の終わりを示す印で、Cコンパイラが”Hello”という文字列をメモリ上に格 納する際に自動的に付加します。  さて、Cで文字列を扱う場合は、次のようにポインタを用いると便利です。 char *str; str = "Hello"; 1

(2)

strはcharへのポインタで、str=”Hello”;により、”Hello”を格納してあるメモリ上の先 頭アドレスがポインタstrに代入されます。つまり、”Hello”という文字列の実体が、str に代入されるわけではありません。  今、ポインタstrは、文字列”Hello”の先頭を指しているので、*strとすると文字’H’を 取り出せます。ここでstr++とポインタを進めると、ポインタ変数strは、次の文字を指 すので、この時*strとすると次の文字’e’を取り出すことになります。 「C言語」(河西朝雄著 ナツメ社)84頁

2.1

例題

15

文字列の文字を1文字ずつ取り出して表示しなさい。 /* 例題15 C言語85頁 文字列の文字を1文字ずつ取り出して表示しなさい。 reidai15.c */ #include <stdio.h> int main() { char *str; str = "Hello"; while(*str != ’\0’){ printf("%c\n", *str); str++; } return 0; } 2

(3)

2.2

練習問題

15

文字列の長さ(\0を含まない)を調べるプログラムを作りなさい。 /* 練習問題15 C言語85頁 文字列の長さ(\0を含まない)を調べるプログラムを作りなさい。 rensyu15.c */ #include <stdio.h> int main() { int n; char *str; str = "Hello"; n = 0; while(*str != ’\0’){ n++; str++; } printf("length = %d\n", n); return 0; } 3

参照

関連したドキュメント

There is a bijection between left cosets of S n in the affine group and certain types of partitions (see Bjorner and Brenti (1996) and Eriksson and Eriksson (1998)).. In B-B,

(The Elliott-Halberstam conjecture does allow one to take B = 2 in (1.39), and therefore leads to small improve- ments in Huxley’s results, which for r ≥ 2 are weaker than the result

[r]

“Breuil-M´ezard conjecture and modularity lifting for potentially semistable deformations after

lines. Notice that Theorem 4 can be reformulated so as to give the mean harmonic stability of the configuration rather than that of the separate foliations. To this end it is

S., Oxford Advanced Learner's Dictionary of Current English, Oxford University Press, Oxford

At the end of the section, we will be in the position to present the main result of this work: a representation of the inverse of T under certain conditions on the H¨older

支払方法 支払日 ※② 緊急時連絡先等 ※③.