# Pointer to object

(object)=
## Object

:::{lrm} object
:fragment: section.3.18
region of data storage that can represent values
:::

For example:

```c
int number = 42;
char name[] = "Partha";
```

- `number` is the identifier of the object `42`. `42` is stored in an data storage area of 4 bytes.
- `name` is the identifier of the object `"Partha"`. This object consists of 7 characters.
<!-- from LRM#page=50 footnotes -->

## We used pointers before

Remember the following diagram from section {ref}`arguments-and-return-value-in-functions`:

:::{mermaid} ../industrial-programming/img/function-input-output.mmd
:::

:::{card} 🤔 Question to ponder
Do you remember
1. what the arrow tips (directions) mean? 
1. an example where a function writes to its arguments?
<!-- scanf: we used pointers (or addresses) to write to arguments -->
:::

## Meaning
<!--
:::{commons-figure} https://commons.wikimedia.org/wiki/File:Pointers.svg
the license parsing does not work well (VERY LONG)
-->
:::{figure} https://upload.wikimedia.org/wikipedia/commons/b/b4/Pointers.svg
:figwidth: 40%
:align: right
A pointer variable `a` which has the value `1008`. `1008` is an address. If we *dereference* `a`, we get the value of the variable `b`.

[Source](https://commons.wikimedia.org/wiki/File:Pointers.svg) CC-BY-SA 3.0 Unported by User:Sven
:::

:::{wpd} pointer
:id: Pointer (computer programming)
an object that stores a memory address
:::

Example:

:::{literalinclude} ../code/a_points_to_b.c
:name: a_points_to_b
:language: c
:::
:::{literalinclude} ../code/a_points_to_b.txt
:::

## Declaration

```c
int *a;  // Pointer to an `int`
char *c;  // Pointer to a `char`

int **d; // Pointer to a pointer to an `int`

float *e = nullptr; // Initialization with zero 
                    // Better than `0`, because `nullptr` identifier shows intent
```
## Usage

:::{list-table} Pointer operators
:header-rows: 1
* - Operator
  - Meaning
  - Example
* - `*`
  - *dereference*
  - `*a`{l=c} gives the value which is stored at the address stored in the pointer variable `a`{l=c}.
* - `&`
  - *address of*
  - `&b`{l=c} gives the address of the value variable `b`{l=c}.
:::
<!--
we don't cover `->` here
-->

:::{exercise} Meaning of pointer operators
:label: meaning-of-pointer-operators
Which are correct for the [code above](project:#a_points_to_b)?

1. `  a == 42`{l=c}
1. ` *a == 42`{l=c}
1. ` &a == 42`{l=c}
1. `&*b == 42`{l=c}
1. `*&b == 42`{l=c}
:::

:::{activity} Declaring and using a pointer
Code the following:
1. Declare and initialize an `unsigned` number called `n`.
1. Declare and initialize a pointer to `n` called `p1`.
1. Declare and initialize a pointer to `n` called `p2`.
1. Print the value that `p1` points to.
1. Increment `n`.
1. Print the value that `p2` points to.

What are printed values?
:::

## Arrays can be used as pointers

:::{literalinclude} ../code/arrays_can_be_used_as_pointers.c
:language: c
:::
:::{literalinclude} ../code/arrays_can_be_used_as_pointers.txt
:language: text
:::
We observe:
- `msg[1]` gives the same value as `*(msg + 1)`.

Note that we can do pointer arithmetic like we do with other types.

We repeat our experiment with an `int` array:

:::{literalinclude} ../code/arrays_can_be_used_as_pointers-with_integer_array.c
:language: c
:::
:::{literalinclude} ../code/arrays_can_be_used_as_pointers-with_integer_array.txt
:language: text
:::

::::{exercise} Pointer arithmetic with `char` vs `int`
:label: pointer-arithmetic-with-char-vs-int
Something is odd. We added 1 to the pointer but the address increased by four 😮. What could be the reason?
:::{dropdown} Hint
What is `sizeof(int)`?
:::
::::

:::{exercise} Calculating `pointer + n`
:label: calculating-pointer-plus-n
1. What would be the difference if you use a `double`? Guess and check your answer by modifying the code above.
2. Can you write a general formula for `pointer + n` on the paper using (1) the size of the data type and (2) address stored in the `pointer`?
:::

## Pointers can be used as arrays

:::{literalinclude} ../code/pointers_can_be_used_as_arrays.c
:language: c
:::
:::{literalinclude} ../code/pointers_can_be_used_as_arrays.txt
:language: text
:::

Pointer does not know its boundaries if used as an array, but an array variable does.

## Pass by address vs pass by value

```c
void set_to_42(int *n);  // pass by address
void set_to_58(int n);  // pass by value. Value is copied and given to the function
```

:::{wpd} pass by address
:id: Evaluation_strategy#Call_by_address
a parameter passing method where the address of the argument is passed.
:::
:::{wpd} pass by value
:id: Evaluation_strategy#Call_by_value
the value of the argument is bound to the corresponding variable in the function typically by copying the value into a new memory region.
:::

:::{activity} Pass by address vs value using scalar values and array
:label: pass-by-address-vs-value-using-scalars-and-array
1. Implement the functions above in using the following template and use them.
1. Guess the output before you run your program.
```{literalinclude} ../code/masked/call_by_address_and_value.c
:language: c
```
:::

:::{warning}
The following argument which tries to get a pointer to the array does not make sense. An array is passed as an address anyway.
```c
void set_first_element_to_42(int *arr[])
```
:::

:::{note}
In literature, you will also see the term *pass by reference* instead of *pass by address*. A reference in C is always an address and an address is always a pointer.

When you talk about it, use *pass by reference* and *pass by value* – every C programmer will understand it. One stands for *having remote access* and the other for *copying the data*.

However if you dig deeper, then you see that in C [everything is pass by value](https://stackoverflow.com/a/78891184/13870816), because the pointers are copied when a function is executed. But it is more important how objects are modified/copied and not pointers themselves.
:::

(copying-an-array-for-passing-by-value)=
## Copying an array for passing-by-value

Arrays are always passed by reference to functions. To create a passing-by-value effect, we have to copy it:

<!-- TODO: I did not have time to live program this -->
::::{card} ⚡ Live programming 
Programming the analogy from {numref}`advantage-of-pointers`.

:::{literalinclude} ../code/birthday_collage.c
:language: c
:::

Why did we not copy the array inside of `careless_friend`? Because then the storage area will not be available.
::::