Even more C

:class: tip This lecture will cover contents from Chapter 2 and Chapter 3 of the book.

```

Addresses and pointers

::::{dropdown} Hands-on: Pointer

1
2
3
ls
gcc -o pointer-1 pointer-1.c
./pointer-1

:::{image} fig/02-c/08.png :alt: Compile and run pointer-1.c :class: bg-primary mb-1 :height: 400px :align: center :::

::::

:::{dropdown} Pointer Definition

:::

::::{dropdown} Hands-on: Pointer and Variable’s Addresses

:::{image} fig/02-c/09.png :alt: Compile and run pointer-2.c :class: bg-primary mb-1 :height: 150px :align: center ::: ::::

::::{dropdown} Pass by Value and Pass by Reference

::::

::::{dropdown} Hands-on: Pass by value

:::{image} fig/02-c/10.png :alt: Compile and run pointer-3.c :class: bg-primary mb-1 :height: 150px :align: center ::: ::::

::::{dropdown} Hands-on: Pass by reference

:::{image} fig/02-c/11.png :alt: Compile and run pointer-4.c :class: bg-primary mb-1 :height: 450px :align: center ::: ::::

:::{dropdown} Answer

Pointers and memory allocation

::::{dropdown} Dynamic memory allocation

::::

::::{dropdown} Void pointer

::::

::::{dropdown} Hands-on: malloc and type cast

:::{image} fig/02-c/12.png :alt: Compile and run malloc-1.c :class: bg-primary mb-1 :height: 150px :align: center ::: ::::

::::{dropdown} Hands-on: malloc and type cast with calculation

:::{image} fig/02-c/13.png :alt: Compile and run malloc-2.c :class: bg-primary mb-1 :height: 150px :align: center ::: ::::

::::{dropdown} Hands-on: Safety

:::{image} fig/02-c/14.png :alt: Compile and run malloc-3.c :class: bg-primary mb-1 :height: 150px :align: center ::: ::::

::::{dropdown} Dynamic memory allocation

Array in C

:::{image} fig/02-c/15.png :alt: Compile and run array-1.c :class: bg-primary mb-1 :height: 250px :align: center :::

:::::{dropdown} Exercise

::::{dropdown} Answer

:::{image} fig/02-c/16.png :alt: Compile and run array-2.c :class: bg-primary mb-1 :height: 250px :align: center ::: :::: :::::

::::{dropdown} An array variable

::::{dropdown} Hands-on: Array as pointer (or vice versa …)

:::{image} fig/02-c/17.png :alt: Compile and run array-3.c :class: bg-primary mb-1 :height: 250px :align: center ::: ::::

Command line arguments.

:::{image} fig/02-c/18.png :alt: Compile and run array-4.c :class: bg-primary mb-1 :height: 250px :align: center :::

String

:::{image} fig/02-c/19.png :alt: Compile and run string-1.c :class: bg-primary mb-1 :height: 150px :align: center :::

:::{dropdown} Answer
24 :::

::::{dropdown} Hands-on: Array of strings

:::{image} fig/02-c/20.png :alt: Compile and run string-2.c :class: bg-primary mb-1 :height: 150px :align: center ::: ::::

Object in C

::::{dropdown} Hands-on: Struct in C

:::{image} fig/02-c/21.png :alt: Compile and run struct-1.c :class: bg-primary mb-1 :height: 150px :align: center ::: ::::

::::{dropdown} Challenge

:::{dropdown} Answer
Insert printf("The address of the origin is: %p\n", &origin); between the existing printf calls.
::: ::::

::::{dropdown} Hands-on: Struct of structs in C

:::{image} fig/02-c/22.png :alt: Compile and run struct-2.c :class: bg-primary mb-1 :height: 150px :align: center ::: ::::

Function in C

::::{dropdown} Hands-on: Functions in C - definition and declaration

:::{image} fig/02-c/23.png :alt: Compile and run function-1.c, function-2.c, function-3.c :class: bg-primary mb-1 :height: 250px :align: center ::: ::::