:class: tip This lecture will cover contents from Chapter 2 and Chapter 3 of the book.
```
& (reference of) represents the memory address of a variable.::::{dropdown} Hands-on: Pointer
intro-c, then use nano to create pointer-1.cwith the source code below.%p is an output conversion syntax (similar to Java specifiers) for displaying memory address in hex format. See Other Output Conversions for more details.pointer-1.c
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
* character.& character in front of a variable (includes pointer variables) denotes that variable’s address location.:::
::::{dropdown} Hands-on: Pointer and Variable’s Addresses
intro-c, then use nano to create pointer-2.cwith the source code below.p_i is a pointer variable, p_i contains a memory address (hence %p).*p_i will point to the value in the memory address contained in p_i. pointer-2.c :::{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
intro-c, then use nano to create pointer-3.cwith the source code below.pointer-3.c :::{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
intro-c, then use nano to create pointer-4.cwith the source code below.pointer-4.c :::{image} fig/02-c/11.png :alt: Compile and run pointer-4.c :class: bg-primary mb-1 :height: 450px :align: center ::: ::::
:::{dropdown} Answer
::::{dropdown} Dynamic memory allocation
malloc void *malloc(size_t size);malloc() function allocates size bytes and returns a pointer to the allocated memory. The memory is not initialized. If size is 0, then malloc() returns either NULL, or a unique pointer value that can later be successfully passed to free().free free() function frees the memory space pointed to by ptr, which must have been returned by a previous call to malloc(), calloc() or realloc(). Otherwise, or if free(ptr) has already been called before, undefined behavior occurs. If ptr is NULL, no operation is performed.::::
::::{dropdown} Void pointer
malloc allocates memory, it returns a sequence of bytes, with no predefined types.::::
::::{dropdown} Hands-on: malloc and type cast
intro-c, then use nano to create malloc-1.cwith the source code below.void *p = malloc(4);: allocate 4 contiguous bytes. The address of the first byte is returned and assign to pointer variable p. p has no type, so it is a void pointer.int *ip = (int *)p;: The address value pointed to by p is assigned to pointer variable ip. The bytes pointed to be p are now casted to type int.malloc-1.c :::{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
intro-c, then use nano to create malloc-2.cwith the source code below.malloc-2.c :::{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
intro-c, then use nano to create malloc-3.cwith the source code below.malloc-3.c :::{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
intro-c, then use nano to create array-1.cwith the source code below.array-1.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
array-1.c called array-2.c.numbers to double.::::{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 …)
intro-c, then use nano to create array-3.cwith the source code below.array-3.c :::{image} fig/02-c/17.png :alt: Compile and run array-3.c :class: bg-primary mb-1 :height: 250px :align: center ::: ::::
intro-c, then use nano to create array-4.cwith the source code below.array-4.c :::{image} fig/02-c/18.png :alt: Compile and run array-4.c :class: bg-primary mb-1 :height: 250px :align: center :::
intro-c, then use nano to create string-1.cwith the source code below.string-1.c :::{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
intro-c, then use nano to create string-2.cwith the source code below.string-2.c :::{image} fig/02-c/20.png :alt: Compile and run string-2.c :class: bg-primary mb-1 :height: 150px :align: center ::: ::::
struct type (think ancestor of objects) .::::{dropdown} Hands-on: Struct in C
intro-c, then use nano to create struct-1.cwith the source code below.struct-1.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
struct-1.c so that it prints out the address of origin variable.:::{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
intro-c, then use nano to create struct-2.cwith the source code below.struct-2.c :::{image} fig/02-c/22.png :alt: Compile and run struct-2.c :class: bg-primary mb-1 :height: 150px :align: center ::: ::::
::::{dropdown} Hands-on: Functions in C - definition and declaration
function-1.c, function-2.c, and function-3.c, with the source codes below::::{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 ::: ::::