diff --git a/slides/pages/makefile-1.md b/slides/pages/makefile-1.md new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/slides/pages/makefile-2.md b/slides/pages/makefile-2.md new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/slides/pages/pointer-1.md b/slides/pages/pointer-1.md index b4f5a63292fea6b63fceb6b32c1558be57f7b15f..f08e61d59daae903b7a3f1a7d6974e01fc1c85e7 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 0000000000000000000000000000000000000000..2f26c626012901a521a6eaff967478fb34ec294d --- /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 0000000000000000000000000000000000000000..c070a5f61b868a946fd5f5e39a678c088d535d42 --- /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 0000000000000000000000000000000000000000..263692b353684ebcee1a8e4d947655a666f5dacc --- /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 ad6fbaccbbda4899bb23aaca5f85cfedba4043d9..eea541a7eaa629a4e1f11f8c9d572d6b804d8f1f 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 +--- +