From a373facb10af10103152b5497c429ae244a16d3c Mon Sep 17 00:00:00 2001
From: Mactavish <maczhanchao@yahoo.com>
Date: Wed, 3 May 2023 16:12:30 +0200
Subject: [PATCH] update slides

---
 slides/pages/makefile-1.md |  0
 slides/pages/makefile-2.md |  0
 slides/pages/pointer-1.md  | 26 ++++++++++++++++++++++++--
 slides/pages/pointer-2.md  | 24 ++++++++++++++++++++++++
 slides/pages/pointer-3.md  | 32 ++++++++++++++++++++++++++++++++
 slides/pages/pointer-4.md  | 38 ++++++++++++++++++++++++++++++++++++++
 slides/slides.md           | 15 +++++++++++++++
 7 files changed, 133 insertions(+), 2 deletions(-)
 create mode 100644 slides/pages/makefile-1.md
 create mode 100644 slides/pages/makefile-2.md
 create mode 100644 slides/pages/pointer-2.md
 create mode 100644 slides/pages/pointer-3.md
 create mode 100644 slides/pages/pointer-4.md

diff --git a/slides/pages/makefile-1.md b/slides/pages/makefile-1.md
new file mode 100644
index 0000000..e69de29
diff --git a/slides/pages/makefile-2.md b/slides/pages/makefile-2.md
new file mode 100644
index 0000000..e69de29
diff --git a/slides/pages/pointer-1.md b/slides/pages/pointer-1.md
index b4f5a63..f08e61d 100644
--- a/slides/pages/pointer-1.md
+++ b/slides/pages/pointer-1.md
@@ -1,5 +1,27 @@
 ---
-title: More Pointers
+title: More on Pointers I
 ---
 
-# More Pointers
+# More on Pointers
+
+## Multiple indirection
+
+Consider the following code:
+
+```c
+int    a =  3;
+int   *b = &a;
+int  **c = &b;
+int ***d = &c;
+```
+
+Here are how the values of these pointers equate to each other:
+
+```c
+  *d ==   c; Dereferencing an (int ***) once gets you an (int **)
+
+ **d ==  *c ==  b; Dereferencing an (int ***) twice, or an (int **) once, gets you an (int *) 
+
+***d == **c == *b == a == 3; Dereferencing an (int ***) thrice, or an (int **) twice, or an (int *) once
+```
+
diff --git a/slides/pages/pointer-2.md b/slides/pages/pointer-2.md
new file mode 100644
index 0000000..2f26c62
--- /dev/null
+++ b/slides/pages/pointer-2.md
@@ -0,0 +1,24 @@
+---
+title: More on Pointers II
+---
+
+## Pointers and `const`
+
+The `const` keyword is used a bit differently when pointers are involved. These two declarations are equivalent:
+
+```c
+// They are both a pointer to a const integer
+// you can not change *ptr_a, but you can change ptr_a
+const int *ptr_a;
+int const *ptr_a;
+```
+
+These two, however, are not equivalent:
+
+```c
+// ptr_a is a pointer to a const integer
+int const *ptr_a;
+// ptr_b is a const pointer to a integer
+// changing *ptr_b is valid, but changing ptr_b is invalid
+int *const ptr_b;
+```
diff --git a/slides/pages/pointer-3.md b/slides/pages/pointer-3.md
new file mode 100644
index 0000000..c070a5f
--- /dev/null
+++ b/slides/pages/pointer-3.md
@@ -0,0 +1,32 @@
+---
+title: More on Pointers III
+---
+
+## Function Pointers
+
+It's possible to take the address of a function, too.
+
+```c
+// Pointer to strcpy-like function
+char *(*strcpy_ptr)(char *dst, const char *src); 
+// Just like in a regular function declaration, the parameter names are optional
+char *(*strcpy_ptr_noparams)(char *, const char *) = strcpy_ptr;
+
+// You can also take the address of a function
+strcpy_ptr = &strcpy;
+strcpy_ptr = strcpy; // this also works 
+```
+
+The type of the pointer to `strcpy` is `char *(*)(char *, const char *)`.
+
+We can even have a function that returns a pointer to functions:
+
+```c
+char *(*get_strcpy_ptr(void))(char *dst, const char *src);
+
+# for better readability, we need to use typedef
+typedef char *(*strcpy_funcptr)(char *, const char *);
+
+strcpy_funcptr strcpy_ptr = strcpy;
+strcpy_funcptr get_strcpy_ptr(void);
+```
diff --git a/slides/pages/pointer-4.md b/slides/pages/pointer-4.md
new file mode 100644
index 0000000..263692b
--- /dev/null
+++ b/slides/pages/pointer-4.md
@@ -0,0 +1,38 @@
+---
+title: More on Pointers IV
+layout: two-cols
+---
+
+## How to Read C Declarations
+
+**The golden rule**:
+
+Start at the variable name (or innermost construct if no identifier
+is present). Look right without jumping over a right parenthesis; say
+what you see. Look left again without jumping over a parenthesis; say
+what you see. Jump out a level of parentheses if any. Look right;
+say what you see. Look left; say what you see. Continue in this
+manner until you say the variable type or return type.
+
+**examples:**
+
+```c
+int i; // an int
+int *a[3];  // an array of size 3 pointers to int
+int (*a)[3]; // an pointer of array of size 3 ints
+int (*Object_vtable[])(); // an array of pointers to function returning int
+```
+
+::right::
+
+## Exercise
+
+Discuss with your teammates and read these declarations out loud:
+
+- `char ****q[ 30 ];`
+- `char **(**q)[ 30 ];`
+- `extern int (x)[];`
+- `long (*a[])( char, char );`
+- `int *(*(*(*b)())[10])();`
+- `char *strprt( char (*)( int ), unsigned char );`
+- `int (*const ab[])( unsigned int );`
diff --git a/slides/slides.md b/slides/slides.md
index ad6fbac..eea541a 100644
--- a/slides/slides.md
+++ b/slides/slides.md
@@ -44,3 +44,18 @@ transition: slide-left
 src: ./pages/pointer-1.md
 ---
 
+---
+transition: slide-left
+src: ./pages/pointer-2.md
+---
+
+---
+transition: slide-left
+src: ./pages/pointer-3.md
+---
+
+---
+transition: slide-left
+src: ./pages/pointer-4.md
+---
+
-- 
GitLab