C prog 2015 11-30

44
C programming ポインタpointer 配列array 型修飾子type modifier

Transcript of C prog 2015 11-30

Page 1: C prog 2015 11-30

C programming ポインタpointer 配列array 型修飾子type modifier

Page 2: C prog 2015 11-30

アジェンダ• 遅刻対応(出席者が揃うまで雑談, 統合開発環境VIM, github, neovim)

• 到達目標 ( 教科書選び, 文法理解, API利用, ライブラリ利用, GLはギリ行けるけどofは無理ぽ ) コピペからコールへ

• C言語学習環境 (gcc, bcc, ideone, VS, slideshare )

• ポインタ

• 配列

• 型修飾子 型変換

• (関数ポインタ)

• やらないこと(オーバーロード ポリモーフィズム ジェネリックプログラミング)

Page 3: C prog 2015 11-30

遅刻対応

• 大事な話なのに遅刻で聞き逃しちゃった!・・・ということが無いように  →関係ない話を10分位

• VIMによるプログラミング支援

Page 4: C prog 2015 11-30

VIM

• NeoVIM

• GitHub

• 分割

Page 5: C prog 2015 11-30

本日の目標

• よくある話

Page 6: C prog 2015 11-30

• 「みんな前の必修の授業でC++やったんだよね?しかもOpenFrameworkも使えるんだね」

Page 7: C prog 2015 11-30

「「じゃあCは完璧じゃん!」」

という誤解が生む悲劇

Page 8: C prog 2015 11-30

• 「じゃあPHPも簡単にできますね」(A先生)

• 「じゃあCLI上でCを扱いましょう」(S先生)

• 「じゃあ何も教えなくても新しい言語出来るね」(T先生)

• 「じゃあメモリが少ない組み込みもCで制御すればいいね」(K先生)

Page 9: C prog 2015 11-30

本日の目標

• 「Cが出来るという誤解による悲劇を減らすためにも少しくらいは、Cが出来るようになろう」(全部は無理)

Page 10: C prog 2015 11-30

本日の目標

• 自分のレベルを知ろう(≒講義のレベル)

• 文法くらいは理解しよう

• 便利なあれこれ(他人の書いたライブラリ)は使えるようになろう

Page 11: C prog 2015 11-30

自分のレベルとは?

• 個人のプログラミングスキルを定量的に測ることは困難である(という事にしましょう。本当はいくつかあって、例えばtopcoderのレーティングとかあるけど。ちなみに僕はグレーとグリーンの間で低空飛行中)

• なのでどんなレベルの教育を受けたか。どんなレベルの参考書を読んだかを立ち位置にします。

Page 12: C prog 2015 11-30

猫でもわかるC言語プログラミング

やさしいC

独習C

ISO/IEC 9899 - Programming languages - C

N1570 Committee Draft — April 12, 2011 ISO/IEC 9899:201x

INTERNATIONAL STANDARD ©ISO/IEC ISO/IEC 9899:201x

Programming languages — C

ABSTRACT

(Cover sheet to be provided by ISO Secretariat.)

This International Standard specifies the form and establishes the interpretation ofprograms expressed in the programming language C. Its purpose is to promoteportability, reliability, maintainability, and efficient execution of C language programs ona variety of computing systems.

Clauses are included that detail the C language itself and the contents of the C languageexecution library. Annexes summarize aspects of both of them, and enumerate factorsthat influence the portability of C programs.

Although this International Standard is intended to guide knowledgeable C languageprogrammers as well as implementors of C language translation systems, the documentitself is not designed to serve as a tutorial.

Recipients of this draft are invited to submit, with their comments, notification of anyrelevant patent rights of which they are aware and to provide supporting documentation.

Changes from the previous draft (N1539) are indicated by ‘‘diff marks’’ in the rightmargin: deleted text is marked with ‘‘∗’’, new or changed text with ‘‘ ’’.

Abstract i

Cのレベル定義(オレオレ版)

C Programming Language

C実践プログラミング

この表に不満/不快感のある人も とても多い気がしますが……

Page 13: C prog 2015 11-30

猫でもわかるC言語プログラミング

やさしいC

独習C

ISO/IEC 9899 - Programming languages - C

N1570 Committee Draft — April 12, 2011 ISO/IEC 9899:201x

INTERNATIONAL STANDARD ©ISO/IEC ISO/IEC 9899:201x

Programming languages — C

ABSTRACT

(Cover sheet to be provided by ISO Secretariat.)

This International Standard specifies the form and establishes the interpretation ofprograms expressed in the programming language C. Its purpose is to promoteportability, reliability, maintainability, and efficient execution of C language programs ona variety of computing systems.

Clauses are included that detail the C language itself and the contents of the C languageexecution library. Annexes summarize aspects of both of them, and enumerate factorsthat influence the portability of C programs.

Although this International Standard is intended to guide knowledgeable C languageprogrammers as well as implementors of C language translation systems, the documentitself is not designed to serve as a tutorial.

Recipients of this draft are invited to submit, with their comments, notification of anyrelevant patent rights of which they are aware and to provide supporting documentation.

Changes from the previous draft (N1539) are indicated by ‘‘diff marks’’ in the rightmargin: deleted text is marked with ‘‘∗’’, new or changed text with ‘‘ ’’.

Abstract i

Cのレベル定義(オレオレ版)

C Programming Language

C実践プログラミング

※個人の感想です。

大人気Webサイトの書籍化。作者は日本人。簡単な課題に対して回答する。文法事項についてはサンプルで出てきた時に必要な部分だけその都度解説。これを読んでも本にあった課題のコピペしか出来ない印象。また、後半のサンプルは難しいし解説不足。 「猫でもわかるシリーズを読んで出来るようになるのは犬の芸」

Page 14: C prog 2015 11-30

猫でもわかるC言語プログラミング

やさしいC

独習C

ISO/IEC 9899 - Programming languages - C

N1570 Committee Draft — April 12, 2011 ISO/IEC 9899:201x

INTERNATIONAL STANDARD ©ISO/IEC ISO/IEC 9899:201x

Programming languages — C

ABSTRACT

(Cover sheet to be provided by ISO Secretariat.)

This International Standard specifies the form and establishes the interpretation ofprograms expressed in the programming language C. Its purpose is to promoteportability, reliability, maintainability, and efficient execution of C language programs ona variety of computing systems.

Clauses are included that detail the C language itself and the contents of the C languageexecution library. Annexes summarize aspects of both of them, and enumerate factorsthat influence the portability of C programs.

Although this International Standard is intended to guide knowledgeable C languageprogrammers as well as implementors of C language translation systems, the documentitself is not designed to serve as a tutorial.

Recipients of this draft are invited to submit, with their comments, notification of anyrelevant patent rights of which they are aware and to provide supporting documentation.

Changes from the previous draft (N1539) are indicated by ‘‘diff marks’’ in the rightmargin: deleted text is marked with ‘‘∗’’, new or changed text with ‘‘ ’’.

Abstract i

Cのレベル定義(オレオレ版)

C Programming Language

C実践プログラミング

※個人の感想です。

初心者向きということでよく教科書になる。文法事項はとりあえず一通り出てくる。なぜか口語。面接で何も分かってない役員に「プログラミング出来ます!」とアピールするならこれで充分。プログラミングを勉強する前段階として1週間で読破して欲しい。 あと高橋麻奈は「やさしいシリーズ」を大量に出してるけど本当に一人で書いてるの?

Page 15: C prog 2015 11-30

猫でもわかるC言語プログラミング

やさしいC

独習C

ISO/IEC 9899 - Programming languages - C

N1570 Committee Draft — April 12, 2011 ISO/IEC 9899:201x

INTERNATIONAL STANDARD ©ISO/IEC ISO/IEC 9899:201x

Programming languages — C

ABSTRACT

(Cover sheet to be provided by ISO Secretariat.)

This International Standard specifies the form and establishes the interpretation ofprograms expressed in the programming language C. Its purpose is to promoteportability, reliability, maintainability, and efficient execution of C language programs ona variety of computing systems.

Clauses are included that detail the C language itself and the contents of the C languageexecution library. Annexes summarize aspects of both of them, and enumerate factorsthat influence the portability of C programs.

Although this International Standard is intended to guide knowledgeable C languageprogrammers as well as implementors of C language translation systems, the documentitself is not designed to serve as a tutorial.

Recipients of this draft are invited to submit, with their comments, notification of anyrelevant patent rights of which they are aware and to provide supporting documentation.

Changes from the previous draft (N1539) are indicated by ‘‘diff marks’’ in the rightmargin: deleted text is marked with ‘‘∗’’, new or changed text with ‘‘ ’’.

Abstract i

Cのレベル定義(オレオレ版)

C Programming Language

C実践プログラミング

※個人の感想です。

やさしいと独習の間に明解とかの有象無象の参考書がたくさん

Page 16: C prog 2015 11-30

猫でもわかるC言語プログラミング

やさしいC

独習C

ISO/IEC 9899 - Programming languages - C

N1570 Committee Draft — April 12, 2011 ISO/IEC 9899:201x

INTERNATIONAL STANDARD ©ISO/IEC ISO/IEC 9899:201x

Programming languages — C

ABSTRACT

(Cover sheet to be provided by ISO Secretariat.)

This International Standard specifies the form and establishes the interpretation ofprograms expressed in the programming language C. Its purpose is to promoteportability, reliability, maintainability, and efficient execution of C language programs ona variety of computing systems.

Clauses are included that detail the C language itself and the contents of the C languageexecution library. Annexes summarize aspects of both of them, and enumerate factorsthat influence the portability of C programs.

Although this International Standard is intended to guide knowledgeable C languageprogrammers as well as implementors of C language translation systems, the documentitself is not designed to serve as a tutorial.

Recipients of this draft are invited to submit, with their comments, notification of anyrelevant patent rights of which they are aware and to provide supporting documentation.

Changes from the previous draft (N1539) are indicated by ‘‘diff marks’’ in the rightmargin: deleted text is marked with ‘‘∗’’, new or changed text with ‘‘ ’’.

Abstract i

Cのレベル定義(オレオレ版)

C Programming Language

C実践プログラミング

※個人の感想です。

ハーバート・シルトの著書の和訳。訳書にしてはまともな訳。だらだらとした説明は無い。明確かつ簡潔に説明される。にもかかわらず最初のサンプルコードが出るまで3ページ以上にわたる文章(それだけ他の書籍は最初に必要な説明を省略している)がある。例題が多い。用語が全て英語も併記されるので後に役に立つ。最初にこれを読むのは難しい。1回読んだだけで理解するのも難しい。しかし教員がこのレベルを求めてる。

Page 17: C prog 2015 11-30

猫でもわかるC言語プログラミング

やさしいC

独習C

ISO/IEC 9899 - Programming languages - C

N1570 Committee Draft — April 12, 2011 ISO/IEC 9899:201x

INTERNATIONAL STANDARD ©ISO/IEC ISO/IEC 9899:201x

Programming languages — C

ABSTRACT

(Cover sheet to be provided by ISO Secretariat.)

This International Standard specifies the form and establishes the interpretation ofprograms expressed in the programming language C. Its purpose is to promoteportability, reliability, maintainability, and efficient execution of C language programs ona variety of computing systems.

Clauses are included that detail the C language itself and the contents of the C languageexecution library. Annexes summarize aspects of both of them, and enumerate factorsthat influence the portability of C programs.

Although this International Standard is intended to guide knowledgeable C languageprogrammers as well as implementors of C language translation systems, the documentitself is not designed to serve as a tutorial.

Recipients of this draft are invited to submit, with their comments, notification of anyrelevant patent rights of which they are aware and to provide supporting documentation.

Changes from the previous draft (N1539) are indicated by ‘‘diff marks’’ in the rightmargin: deleted text is marked with ‘‘∗’’, new or changed text with ‘‘ ’’.

Abstract i

Cのレベル定義(オレオレ版)

C Programming Language

C実践プログラミング

※個人の感想です。

このへんにある「中級者向けステップアップ用」な書籍から適当に1冊ピックアップした。私がこれを読んでいないのでノーコメント。 「内容としては簡単な内容から少々難しめな内容を題材に実践的なCによるプログラムの書き方を示しています。本書における”実践的”とは”保守しやすい”と言う事です。ほぼ全ての説明がこの観点から書かれています。」(amazon.co.jp レビューより)

Page 18: C prog 2015 11-30

猫でもわかるC言語プログラミング

やさしいC

独習C

ISO/IEC 9899 - Programming languages - C

N1570 Committee Draft — April 12, 2011 ISO/IEC 9899:201x

INTERNATIONAL STANDARD ©ISO/IEC ISO/IEC 9899:201x

Programming languages — C

ABSTRACT

(Cover sheet to be provided by ISO Secretariat.)

This International Standard specifies the form and establishes the interpretation ofprograms expressed in the programming language C. Its purpose is to promoteportability, reliability, maintainability, and efficient execution of C language programs ona variety of computing systems.

Clauses are included that detail the C language itself and the contents of the C languageexecution library. Annexes summarize aspects of both of them, and enumerate factorsthat influence the portability of C programs.

Although this International Standard is intended to guide knowledgeable C languageprogrammers as well as implementors of C language translation systems, the documentitself is not designed to serve as a tutorial.

Recipients of this draft are invited to submit, with their comments, notification of anyrelevant patent rights of which they are aware and to provide supporting documentation.

Changes from the previous draft (N1539) are indicated by ‘‘diff marks’’ in the rightmargin: deleted text is marked with ‘‘∗’’, new or changed text with ‘‘ ’’.

Abstract i

Cのレベル定義(オレオレ版)

C Programming Language

C実践プログラミング

※個人の感想です。

ブライアン・カーニハンとデニス・リッチーの共著。いわゆるK&R。C言語の標準規格の元となる。故に間違った知識を植え付けられることはまずない。一方で規格の指針になっているだけで実践的でも無い。また古い。日本語の訳書も出てる(が誤訳がある。) C言語のバイブルと崇められ、ひいてはコンピュータのバイブルとも言えるだろう書籍なので死ぬまでに1度は読んでおきたい。コレを読むとベテランさんの昔話の時に人気者になれる。

Page 19: C prog 2015 11-30

猫でもわかるC言語プログラミング

やさしいC

独習C

ISO/IEC 9899 - Programming languages - C

N1570 Committee Draft — April 12, 2011 ISO/IEC 9899:201x

INTERNATIONAL STANDARD ©ISO/IEC ISO/IEC 9899:201x

Programming languages — C

ABSTRACT

(Cover sheet to be provided by ISO Secretariat.)

This International Standard specifies the form and establishes the interpretation ofprograms expressed in the programming language C. Its purpose is to promoteportability, reliability, maintainability, and efficient execution of C language programs ona variety of computing systems.

Clauses are included that detail the C language itself and the contents of the C languageexecution library. Annexes summarize aspects of both of them, and enumerate factorsthat influence the portability of C programs.

Although this International Standard is intended to guide knowledgeable C languageprogrammers as well as implementors of C language translation systems, the documentitself is not designed to serve as a tutorial.

Recipients of this draft are invited to submit, with their comments, notification of anyrelevant patent rights of which they are aware and to provide supporting documentation.

Changes from the previous draft (N1539) are indicated by ‘‘diff marks’’ in the rightmargin: deleted text is marked with ‘‘∗’’, new or changed text with ‘‘ ’’.

Abstract i

Cのレベル定義(オレオレ版)

C Programming Language

C実践プログラミング

※個人の感想です。

C言語の規格書。これを読んだから偉いという訳でも無いが、正しいことは全てここにある。(もっとも、全てのコンパイラが正しく規格を実装するのは不可能である。さらに規格に無い事が多くある)

無理して読むものでも無いが、困ったときに正解を知るためには読むべきもの。これを和訳したJIS X3010もある。

Page 20: C prog 2015 11-30

猫でもわかるC言語プログラミング

やさしいC

独習C

ISO/IEC 9899 - Programming languages - C

N1570 Committee Draft — April 12, 2011 ISO/IEC 9899:201x

INTERNATIONAL STANDARD ©ISO/IEC ISO/IEC 9899:201x

Programming languages — C

ABSTRACT

(Cover sheet to be provided by ISO Secretariat.)

This International Standard specifies the form and establishes the interpretation ofprograms expressed in the programming language C. Its purpose is to promoteportability, reliability, maintainability, and efficient execution of C language programs ona variety of computing systems.

Clauses are included that detail the C language itself and the contents of the C languageexecution library. Annexes summarize aspects of both of them, and enumerate factorsthat influence the portability of C programs.

Although this International Standard is intended to guide knowledgeable C languageprogrammers as well as implementors of C language translation systems, the documentitself is not designed to serve as a tutorial.

Recipients of this draft are invited to submit, with their comments, notification of anyrelevant patent rights of which they are aware and to provide supporting documentation.

Changes from the previous draft (N1539) are indicated by ‘‘diff marks’’ in the rightmargin: deleted text is marked with ‘‘∗’’, new or changed text with ‘‘ ’’.

Abstract i

Cのレベル定義(オレオレ版)

C Programming Language

C実践プログラミング

※個人の感想です。

おそらく半年の講義で、「アートな成果物を作れ」とまで言われて到達できるのはここ。もしかしたらそれ以下。

Page 21: C prog 2015 11-30

猫でもわかるC言語プログラミング

やさしいC

独習C

ISO/IEC 9899 - Programming languages - C

N1570 Committee Draft — April 12, 2011 ISO/IEC 9899:201x

INTERNATIONAL STANDARD ©ISO/IEC ISO/IEC 9899:201x

Programming languages — C

ABSTRACT

(Cover sheet to be provided by ISO Secretariat.)

This International Standard specifies the form and establishes the interpretation ofprograms expressed in the programming language C. Its purpose is to promoteportability, reliability, maintainability, and efficient execution of C language programs ona variety of computing systems.

Clauses are included that detail the C language itself and the contents of the C languageexecution library. Annexes summarize aspects of both of them, and enumerate factorsthat influence the portability of C programs.

Although this International Standard is intended to guide knowledgeable C languageprogrammers as well as implementors of C language translation systems, the documentitself is not designed to serve as a tutorial.

Recipients of this draft are invited to submit, with their comments, notification of anyrelevant patent rights of which they are aware and to provide supporting documentation.

Changes from the previous draft (N1539) are indicated by ‘‘diff marks’’ in the rightmargin: deleted text is marked with ‘‘∗’’, new or changed text with ‘‘ ’’.

Abstract i

Cのレベル定義(オレオレ版)

C Programming Language

C実践プログラミング

※個人の感想です。

ところがOpenGLで画像処理をしたり、Cの知識を前提にC++で制御を行ったり、他の言語をハイスピードに学んだり・・・ というレベルはここ

Page 22: C prog 2015 11-30

猫でもわかるC言語プログラミング

やさしいC

独習C

ISO/IEC 9899 - Programming languages - C

N1570 Committee Draft — April 12, 2011 ISO/IEC 9899:201x

INTERNATIONAL STANDARD ©ISO/IEC ISO/IEC 9899:201x

Programming languages — C

ABSTRACT

(Cover sheet to be provided by ISO Secretariat.)

This International Standard specifies the form and establishes the interpretation ofprograms expressed in the programming language C. Its purpose is to promoteportability, reliability, maintainability, and efficient execution of C language programs ona variety of computing systems.

Clauses are included that detail the C language itself and the contents of the C languageexecution library. Annexes summarize aspects of both of them, and enumerate factorsthat influence the portability of C programs.

Although this International Standard is intended to guide knowledgeable C languageprogrammers as well as implementors of C language translation systems, the documentitself is not designed to serve as a tutorial.

Recipients of this draft are invited to submit, with their comments, notification of anyrelevant patent rights of which they are aware and to provide supporting documentation.

Changes from the previous draft (N1539) are indicated by ‘‘diff marks’’ in the rightmargin: deleted text is marked with ‘‘∗’’, new or changed text with ‘‘ ’’.

Abstract i

Cのレベル定義(オレオレ版)

C Programming Language

C実践プログラミング

※Cで仕事してるって人に実際に会った事がないです

さらに実際にCで仕事をしたりという人はこのレベルの勉強を日々してるんじゃないかなぁ(という想像)

Page 23: C prog 2015 11-30

• 「プログラミングは目的じゃなくて手段だから。大事なのはそれを使って出来るモノ(単位)だから」 と言って猫以下のレベルに満足していいのか?

Page 24: C prog 2015 11-30

• 「プログラミングは目的じゃなくて手段だから。大事なのはそれを使って出来るモノ(単位)だから」 と言って猫以下のレベルに満足していいのか?

• という事を伝えたいと思っています。

• スライドはWebにアップロードします。 → tweet, シェア は大歓迎

Page 25: C prog 2015 11-30

猫でもわかるC言語プログラミング

やさしいC

独習C

ISO/IEC 9899 - Programming languages - C

N1570 Committee Draft — April 12, 2011 ISO/IEC 9899:201x

INTERNATIONAL STANDARD ©ISO/IEC ISO/IEC 9899:201x

Programming languages — C

ABSTRACT

(Cover sheet to be provided by ISO Secretariat.)

This International Standard specifies the form and establishes the interpretation ofprograms expressed in the programming language C. Its purpose is to promoteportability, reliability, maintainability, and efficient execution of C language programs ona variety of computing systems.

Clauses are included that detail the C language itself and the contents of the C languageexecution library. Annexes summarize aspects of both of them, and enumerate factorsthat influence the portability of C programs.

Although this International Standard is intended to guide knowledgeable C languageprogrammers as well as implementors of C language translation systems, the documentitself is not designed to serve as a tutorial.

Recipients of this draft are invited to submit, with their comments, notification of anyrelevant patent rights of which they are aware and to provide supporting documentation.

Changes from the previous draft (N1539) are indicated by ‘‘diff marks’’ in the rightmargin: deleted text is marked with ‘‘∗’’, new or changed text with ‘‘ ’’.

Abstract i

Cのレベル定義(オレオレ版)

C Programming Language

C実践プログラミング

このへんのレベルに行けるようにというところで不足しがちなところを補足します。

Page 26: C prog 2015 11-30

C言語環境• 時間がなさそうなので省略自前でVisualC++とかインストールして欲しい

• とりあえずサンプルコード動かすだけなら ideone とかオススメ

• スライドはhttp://www.slideshare.net/*********/**********

Page 27: C prog 2015 11-30

今回補足する文法事項(やっと本題)

• ポインタ/配列

• 型修飾子/型変換

• (関数ポインタ)

• 本当はこれもやりたかった→オーバーロード・ポリモーフィズム(OpenFrameworksとかで使うやつ)ジェネリックプログラミング(C++で便利なSTLに使う)

Page 28: C prog 2015 11-30

ポインタ• ポインタを理解するためにはプログラムがメモリ領域をどのように使っているのかを理解する必要がある。(逆にC言語はメモリ領域をどう使っているかが比較的明解な言語とも言える)

• 以下のコードは次の表の ようにイメージできる

char a =’s’; char b[4] = { ’n’, ’o’, ’o’, ’b’}; int c = 47; int d[2] = { 10, -3};

https://ideone.com/keqP01

Page 29: C prog 2015 11-30

char a =’s’; char b[4] = { ’n’, ’o’, ’o’, ’b’}; int c = 47; int d[2] = { 10, -3};

ab

c

d

※ビッグエンディアン ※説明簡略化のため1番地の空白が規則的にあるが 実際はどこに配置されるかに規則性は無い 開始番地も不定である。

Page 30: C prog 2015 11-30

char a =’s’; char b[4] = { ’n’, ’o’, ’o’, ’b’}; int c = 47; int d[2] = { 10, -3};

かなり不正確な表記 (本当は数字も文字も内部的に区別はない)

Page 31: C prog 2015 11-30

ポインタ• ポインタを理解するためにはプログラムがメモリ領域をどのように使っているのかを理解する必要がある。(逆にC言語はメモリ領域をどう使っているかが比較的明解な言語とも言える)

• 以下のコードは次の表の ようにイメージできる

char a =’s’; char b[4] = { ’n’, ’o’, ’o’, ’b’}; int c = 47; int d[2] = { 10, -3};

※ビッグエンディアン ※説明簡略化のため1番地の空白が規則的にあるが 実際はどこに配置されるかに規則性は無い 開始番地も不定である。

Page 32: C prog 2015 11-30

ポインタ• ポインタを理解するためにはプログラムがメモリ領域をどのように使っているのかを理解する必要がある。(逆にC言語はメモリ領域をどう使っているかが比較的明解な言語とも言える)

• 以下のコードは次の表の ようにイメージできる

char a =’s’; char b[4] = { ’n’, ’o’, ’o’, ’b’}; int c = 47; int d[2] = { 10, -3};

※ビッグエンディアン ※説明簡略化のため1番地の空白が規則的にあるが 実際はどこに配置されるかに規則性は無い 開始番地も不定である。

↓まさにこの番地がポインタの   指すものである

Page 33: C prog 2015 11-30

ポインタ(こんなの復習だよね?)

• ポインタ変数はchar* p; や char *p; のような形で宣言できた。

• 逆にある変数をchar a; のように宣言したらその番地(address)は&a のように書けば取得できる

Page 34: C prog 2015 11-30

char a =’s’; char b[4] = { ’n’, ’o’, ’o’, ’b’}; int c = 47; int d[2] = { 10, -3};

ab

c

d

※ビッグエンディアン ※説明簡略化のため1番地の空白が規則的にあるが 実際はどこに配置されるかに規則性は無い 開始番地も不定である。

&a&b

&c

&d

ということは、 それぞれの変数の 番地はこうなる

Page 35: C prog 2015 11-30

ポインタ• じゃあ実際に&aを見ようとすると

https://ideone.com/wioEwN

実行する度に番地が違う

Page 36: C prog 2015 11-30

ポインタ(こんなの復習だよね?)

• ポインタ変数は 参照渡し に使われる。

• ということは次のような事故が起きることがある。

Page 37: C prog 2015 11-30

https://ideone.com/SV9new

Page 38: C prog 2015 11-30

https://ideone.com/oRR2xy

Page 39: C prog 2015 11-30

• というように参照渡しでは引数に指定した値の書き換えが起きる

• ということが意図していたならいいが、事故で書き換わるかもしれない。

• そこでconst 修飾子(前回話題になって飛ばした)

• const 修飾子は「書き換え禁止(read only)」の属性を持たせる

Page 40: C prog 2015 11-30

https://ideone.com/PxOPV1

こういう感じでエラーになってくれる

Page 41: C prog 2015 11-30

https://ideone.com/mTAKDs

対策するならこんな感じ

Page 42: C prog 2015 11-30

https://ideone.com/mTAKDs

Page 43: C prog 2015 11-30

• というのが説明しなかったconst修飾子のお話でした。

• じゃあなんで型が違ってても大丈夫なの?→正解を見つけられませんでした。

Page 44: C prog 2015 11-30

関数ポインタ(おまけ)

• OpenGLプログラミングを例に解説しますhttp://wisdom.sakura.ne.jp/system/opengl/