⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 17.txt

📁 一個計算機系教授的上課講義 主要是教導學生使用C語言編寫程序
💻 TXT
字号:
CS 1355
Introduction to Programming in C
Monday 2006.11.6 (Week 9)
Lecture notes (at http://r638-2.cs.nthu.edu.tw/C/notes/17.txt)

Chapter 7: Pointers (continued)

Function Pointers

So far: pointers are for data
This time: pointers to functions

Example:
	printf("hello world\n"); /* this is a function call */
	printf	=> this is a expression = address of the function!

Why function pointer?
Reason: a "plug-in" function

Example application: sorting
- sorting can be done (1) in increasing order (2) in decreasing order
  increasing:  1 2 3 4 5 6 7 8 9 10
  decreasing: 10 9 8 7 6 5 4 3 2 1
- difference: comparison
  increasing: use "greater than" comparison (when needing to swap)
  decreasing: use "less than" comparison (when needing a swap)
  relational operator: yields true/false value

int greaterThan(int a, int b) {
	return a > b;
}
int lessThan(int a, int b) {
	return a < b;
}

in bubble sort, previously (See Lecture 13) we had 

1	void
2	bubbleUp(int a[], int i) {
3		if (a[i] > a[i+1]) {
4			int temp = a[i];
5			a[i] = a[i+1];
6			a[i+1] = temp;
7		}
8	}

Line 3 can be replaced with
	if (greaterThan(a[i], a[i+1])) { ... }

How does it help to replace > with greaterThan()?
=> because we can pass a pointer instead of hardwiring the
   comparison function!

How to declare a function pointer:
	int (*compare)(int a, int b);
=> this declares "compare" as a variable to a function that
   takes two parameters (int a, int b)
   and returns an int

once declared, you can do
	compare = greaterThan;
or
	compare = lessThan;
=> because both greaterThan() and lessThan() have the same "type signature"!
   (note: the names a, b don't have to match; just the data types 
         in the same sequence)
To call a function pointer, dereference it like before,
except put it in ( ).
	(*compare)(a, b);

You can also pass it as a parameter!

Example: enhanced bubbleUp()
void
bubbleUp(int a[], int i, int (*compare)(int a, int b)) {
	if ((*compare)(a[i], a[i+1])) {
		int temp = a[i];
		a[i] = a[i+1];
		a[i+1] = temp;
	}
}

We can declare bubbleSort() as

void bubbleSort(int a[], int size, int (*compare)(int a, int b)) {
	int i, j;
	for (i = size-1; i > 0; i--) {
		for (j = 0; j < i; j++) {
			bubbleUp(a, j, compare);
			/* instead of bubbleUp(a, j) */
		}
	} 
}

This way, we can reuse the same code for both
 ascending (increasing) order and
 descending (decreasing) order!

	bubbleSort(a, size, greaterThan); /* sort by increasing order */
	bubbleSort(a, size, lessThan); /* sort by decreasing order */

Another use of function pointers: 
- array of function pointers, as an alternative to switch statements!
	int i, x, y;
	scantf("%d%d%d", &i, &x, &y);
	switch(i) {
	case 0: f1(x, y); break;
	case 1: f2(x, y); break;
	case 2: f3(x, y); break;
	case 3: f4(x, y); break;
	case 4: f5(x, y); break;
	default: fd(x, y);
	}
=> this can be rewritten as an array of pointers to functions

	int (*fp[ ])(int a, int b) = {
		f1, f2, f3, f4, f5, fd
	};
	int i, x, y;
	scanf("%d%d%d", &i, &x, &y);
	if (i < 0 || i > 4) { /* the "default" case */
		fd(x, y);
	} else {
		(*fp)(x, y);
	}

Extra; argc and argv:

int main(int argc, char *argv[ ]) {
	int i;
	for (i = 0; i < argc; i++) {
		printf("%s\n", argv[i]);
	}
}

=> what does this mean?
   argc means "argument count",
   argv means "argument value"
=> these are the "arguments" from the command line!

so, if you run the program as
% ./a.out abc xyz
=> then this has 
	argc = 3;
	argv = { "./a.out", "abc", "xyz" };
These are useful for passing arguments to a program.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -