Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Small Corrections for Chapter 6 #126

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
36 changes: 18 additions & 18 deletions Chapters/05-pointers.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ std.debug.print("{d}\n", .{doubled});


This syntax to dereference the pointer is nice. Because we can easily chain it with
methods of the value pointed by the pointer. We can use the `User` struct that we have
created at @sec-structs-and-oop as an example. If you comeback to that section,
you will see that this struct have a method named `print_name()`.
methods of the value pointed by the pointer. We can use the `User` struct that we
created in @sec-structs-and-oop as an example. If you come back to that section,
you will see that this struct has a method named `print_name()`.

So, for example, if we have an user object, and a pointer that points to this user object,
we can use the pointer to access this user object, and, at the same time, call the method `print_name()`
Expand Down Expand Up @@ -126,7 +126,7 @@ one of main reasons why pointers are used in programming languages.
You can have a pointer that points to a constant object, or, a pointer that points to a variable object.
But regardless of who this pointer is, a pointer **must always respect the characteristics of the object that it points to**.
As a consequence, if the pointer points to a constant object, then, you cannot use this pointer
to change the value that it points to. Because it points to a value that is constant. As we discussed at @sec-assignments, you cannot
to change the value that it points to. Because it points to a value that is constant. As we discussed in @sec-assignments, you cannot
change a value that is constant.

For example, if I have a `number` object, which is constant, I cannot execute
Expand Down Expand Up @@ -178,7 +178,7 @@ We can have a constant pointer that points to a constant value.
But we can also have a variable pointer that points to a constant value. And vice-versa.

Until this point, the `pointer` object was always constant,
but what this means for us? What is the consequence of the
but what does this mean for us? What is the consequence of the
`pointer` object being constant? The consequence is that
we cannot change the pointer object, because it is constant. We can use the
pointer object in multiple ways, but we cannot change the
Expand Down Expand Up @@ -220,10 +220,10 @@ In Zig, there are two types of pointers [@zigdocs], which are:


Single-item pointer objects are objects whose data types are in the format `*T`.
So, for example, if an object have a data type `*u32`, it means that, this
So, for example, if an object has the data type `*u32`, it means that this
object contains a single-item pointer that points to an unsigned 32-bit integer value.
As another example, if an object have type `*User`, then, it contains
a single-item pointer to an `User` value.
As another example, if an object has type `*User`, then it contains
a single-item pointer to a `User` value.

In contrast, many-item pointers are objects whose data types are in the format `[*]T`.
Notice that the star symbol (`*`) is now inside a pair of brackets (`[]`). If the star
Expand Down Expand Up @@ -270,7 +270,7 @@ try stdout.print("{d}\n", .{ptr[0]});

Although you can create a pointer to an array like that, and
start to walk through this array by using pointer arithmetic,
in Zig, we prefer to use slices, which were presented at @sec-arrays.
in Zig, we prefer to use slices, which were presented in @sec-arrays.

Behind the hood, slices already are pointers,
and they also come with the `len` property, which indicates
Expand All @@ -280,7 +280,7 @@ can use it to check for potential buffer overflows, and other problems like that
Also, you don't need to use pointer arithmetic to walk through the elements
of a slice. You can simply use the `slice[index]` syntax to directly access
any element you want in the slice.
As I mentioned at @sec-arrays, you can get a slice from an array by using
As I mentioned in @sec-arrays, you can get a slice from an array by using
a range selector inside brackets. In the example below, I'm creating
a slice (`sl`) that covers the entire `ar` array. I can access any
element of `ar` from this slice, and, the slice itself already is a pointer
Expand All @@ -307,7 +307,7 @@ might point to a null value. This is a common source of undefined behaviour in C
When programmers work with pointers in C, they have to constantly check if
their pointers are pointing to null values or not.

If for some reason, your Zig code produces a null value somewhere, and, this null
If for some reason your Zig code produces a null value somewhere, and this null
value ends up in an object that is non-nullable, a runtime error is always
raised by your Zig program. Take the program below as an example.
The `zig` compiler can see the `null` value at compile time, and, as result,
Expand All @@ -330,8 +330,8 @@ p5.zig:5:14: error: expected type 'u8',

You don't get this type of safety in C.
In C, you don't get warnings or errors about null values being produced in your program.
If for some reason, your code produces a null value in C, most of the times, you end up getting a segmentation fault error
as result, which can mean many things.
If for some reason your code produces a null value in C, most of the time you end up getting a segmentation fault error
as a result, which can mean many things.
That is why programmers have to constantly check for null values in C.

Pointers in Zig are also, by default, **non-nullable**. This is another amazing
Expand All @@ -353,7 +353,7 @@ this `?` operator right before the data type of an object, you transform
this data type into an optional data type, and the object becomes an optional object.

Take the snippet below as an example. We are creating a new variable object
called `num`. This object have the data type `?i32`, which means that,
called `num`. This object has the data type `?i32`, which means that,
this object contains either a signed 32-bit integer (`i32`), or, a null value.
Both alternatives are valid values to the `num` object.
That is why, I can actually change the value of this object to null, and,
Expand Down Expand Up @@ -401,7 +401,7 @@ a pointer to an optional value. In other words, a pointer to a value that is eit
null value, or, a not-null value.

In the example below, we are recreating this idea. Now, the `ptr` object
have a data type of `*?i32`, instead of `?*i32`. Notice that the `*` symbol comes before of `?`
has a data type of `*?i32`, instead of `?*i32`. Notice that the `*` symbol comes before of `?`
this time. So now, we have a pointer that points to a value that is either null
, or, a signed 32-bit integer.

Expand All @@ -419,7 +419,7 @@ _ = ptr;

When you have an optional object in your Zig code, you have to explicitly handle
the possibility of this object being null. It is like error-handling with `try` and `catch`.
In Zig you also have to handle null values like if they were a type of error.
In Zig, you also have to handle null values like if they were a type of error.

We can do that, by using either:

Expand Down Expand Up @@ -451,8 +451,8 @@ in a null value, and on the right side of `orelse`, you provide another expressi
that will not result in a null value.

The idea behind the `orelse` keyword is: if the expression on the left side
result in a not-null value, then, this not-null value is used. However,
if this expression on the left side result in a null value, then, the value
results in a not-null value, then this not-null value is used. However,
if this expression on the left side results in a null value, then the value
of the expression on the right side is used instead.

Looking at the example below, since the `x` object is currently null, the
Expand Down