Skip to content
Snippets Groups Projects
Commit a373facb authored by Mactavish's avatar Mactavish
Browse files

update slides

parent 17ef4581
Branches
No related tags found
No related merge requests found
---
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
```
---
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;
```
---
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);
```
---
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 );`
......@@ -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
---
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment