Applications of pointers II: References, Indirection

(ポインタの応用 I: 参照、間接)

Computing Practice I

11th lecture, June 29, 2017

http://www.sw.it.aoyama.ac.jp/2017/CP1/lecture11.html

Martin J. Dürst

AGU

© 2005-17 Martin J. Dürst 青山学院大学

Today's Schedule

 

ミニテスト

 

Summary of Last Lecture

 

Results of Previous Exercises

10A1 10B2 10C1 10C2
100 points 97 50 32 4
60 points 1 48 62 32
partial 1 48 62 17
errors - - 3 34
not submitted 1 1 2 13

 

10C2 の注意点

 

Usages of Pointers

  1. Low-level address manipulation (device allocation,...)
  2. Speedup of array processing
  3. Dynamic memory management
  4. Reference (passing of arguments to a function by reference,...)
  5. Indirection

  

References in Natural Language

 

引数の値渡しと参照渡しのイメージ

値渡し (call by value):

  1. 付箋に値を書いて、捨ててもいいように渡す
  2. 修理するものを直接渡す (例: 靴の修理)

参照渡し (call by reference):

  1. 箱の中に値を用意し、箱の場所を渡す
  2. 修理する場所 (住所) を指定 (例: 水道管の修理)

 

Call by Reference

Pattern for Call by Reference (Except Arrays)

 

Pattern for Call by Reference (Arrays)

 

How to Pass Arrays by Value

  1. Call by reference, do not change in callee
  2. As in (1), but add const qualifiers to declare and assure there are no changes
  3. Copy data before passing it
  4. Wrap the array in a struct, and pass the struct (which is passed by value)

 

Application of Call by Reference: Multiple Return Values

Problem: Create a function that returns both n2 and n3

Solution: Pass uninitialized variables by reference:

void square_cube(int n, int *pn2, int *pn3)
{
*pn2 = n * n;
*pn3 = *pn2 * n;
}

Caller side:

int a=5, a2, a3;
square_cube(a, &a2, &a3);
printf("a=%d, a*a=%d, a*a*a=%d\n", a, a2, a3);

 

Indirection

名言: 「全ての情報テクノロジーの問題は間接のレベルを一つ増やせば解決可能」

間接の例:

C の場合には間接をポインタで実現

 

Applications of Indirection

順番を入れ替える時に、実際のデータを入れ替えると:

間接を使えば全ての問題が解決 (例: 11C2)

 

Functions as Arguments

(pointers to) functions can be passed as arguments to other functions

Typical example: Execute some 'work' according to some pattern, where the 'work' details are not fixed

Application example: Execute some work (e.g. clean blackbords, clean room, switch off lights, check air quality, check fire alarms...) in all lecture halls of Sagamihara campus

Solution:

 

How to Implement Sorting

 

Introducing Comparison Functions

 

Functions as Arguments in Other Programming Languages

 

Example of Difference between Languages

Ruby: students.sort { |a, b| a.number <=> b.number }

Or even easier: students.sort_by { |a| a.number }

Shorthand: students.sort_by &:number

C (from 11C2):

int compareNumbers (const void* a, const void* b)
{
Student *studentPa = *((Student **) a);
Student *studentPb = *((Student **) b);

return strcmp (studentPa->studentNumber,
studentPb->studentNumber);
}
... qsort(byNumber, studentCount, sizeof(Student*), compareNumbers);

 

今日の演習

穴埋めや追加が多いので、インデントに注意

 

次回の準備

 

Glossary

call (pass) by reference
参照渡し
array index
配列の指数
callee side
(呼ばれた) 関数側
caller side
呼び出し側
sort(ing)
ソート、整列
(sort) criterion
(整列の) 基準
bubble sort
バブル整列
insertion sort
挿入整列
selection sort
選択整列
quicksort
クイックソート
comparison function
比較関数