Even more C

Even more C

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

## Addresses and pointers


- In Java, you can manipulate the value of a variable via the program but not directly 
in memory (inside the JVM). 
- In C, you can retrieve the address of the location in memory where the variable is 
stored. 
- The operator `&` (reference of) represents the memory address of a variable.



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

- Inside the terminal, make suse that you are still inside
`intro-c`, then use `nano` to create `pointer-1.c`with the source code below. 

<script src="https://gist.github.com/linhbngo/d1e9336a82632c528ea797210ed0f553.js?file=pointer-1.c"></script>

- `%p` is an output conversion syntax (similar to Java specifiers) for displaying memory 
address in hex format. See [Other Output Conversions](https://www.gnu.org/software/libc/manual/html_node/Other-Output-Conversions.html#Other-Output-Conversions) for more 
details.
- Compile and run `pointer-1.c`

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 ::: ::::