Skip to content

Commit

Permalink
vac
Browse files Browse the repository at this point in the history
  • Loading branch information
snarlferb committed Aug 5, 2024
1 parent 6357074 commit 6b0b750
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 13 deletions.
4 changes: 3 additions & 1 deletion arr.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
</head><body>

<div id="arr-anch">--- fly^p4rray ---</div>

<span class="alt-text">
int arr[3];</span>

Expand Down Expand Up @@ -270,7 +271,8 @@
}</span>

7mod5 equals 2 and, following along w/ the rest of the logic, why does it say the array variable's value is "3"?
hint: it was one of the very first things we said about arrays and their unique properties, and should be obvious

It was one of the very first things we said about arrays and their unique properties, and should be obvious
you're not counting over twice, but looking at the arrays actual <u>index#2</u>, and the element of that position.

see <a class="reserve" href="malloc.html">malloc</a> on page8, or see <a class="reserve" href="struct.html">struct</a> on page5
Expand Down
4 changes: 2 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
<a class="hl-hover" href="func.html#ptr-anch"> 439l &#9656;pointer</a>
<a class="hl-hover" href="func.html#variadic-anch"> 439l &#9656;variadic function</a>
<a class="hl-hover" href="func.html#function-pointer-anch"> 439l &#9656;function pointer u graduate from the triathlon upon reaching this point</a>
<a class="hl-hover" href="arr.html#arr-anch"> 278l &#9656;array ▂___ </a>
<a class="hl-hover" href="struct.html#struct-anch"> 260l &#9656;struct</a>
<a class="hl-hover" href="arr.html#arr-anch"> 280l &#9656;array ▂___ </a>
<a class="hl-hover" href="struct.html#struct-anch"> 280l &#9656;struct</a>
<a class="hl-hover" href="macro.html#macro-anch"> 312l &#9656;macro</a>
<div style="position: absolute;background-color: #222222; cursor: pointer; display: flex; align-items: center; justify-content: center;" onclick="window.location.href='compiler.html#compiler-anch'">&#8594;
.-=========-.
Expand Down
40 changes: 30 additions & 10 deletions struct.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,15 @@
in technical terms they would be described as contiguous blocks of memory,
wherein the fields (members) are accessed using offsets.
<span class="alt-text">
+---------------+
| x | y |</span>

x is at offset 0 within the struct
y is at offset `sizeof(int)` bytes within the struct
x is at offset 0 to (e.g. 3), 4 bytes within the struct (the start of the struct)
y is at offset `sizeof(int)`, presumably 1 byte, afterwards.
Padding may be added after y to align the structures size.
In this simple case, the structure could align itself naturally to
the largest member's alignment requirement. Otherwise the padding after y
would be 3 bytes to align the total size to the next multiple of 4.

structs can store any data type and
you can create them like this
Expand All @@ -39,13 +44,28 @@

When you see this `Object.Member, its referred to as a Member that is
being accessed through the pointer to Object... you use the dot when there's
direct access, and the (-&gt;) arrow operator when its indirect (i.e. when it's
being accessed from a pointer already)...
`newname.x` could be declared w/ direct access
`something-&gt;x` would have indirect access, seeing
as its two levels away from `example`
direct access (used with a struct variable and not a pointer) and the (-&gt;)
arrow operator when its indirect (i.e. when it's being accessed from a pointer already)

you can declare a struct like this
`newname.x` could be declared w/ direct access, and `something-&gt;x` would have
indirect access, seeing as its two levels away from `example`

Because arrow operator is used to access members of a struct through a pointer
to that struct, its combining a dereference of the pointer with accessing of the struct
member all in one step.
<span class="alt-text">
List *this = &that;
this-&gt;num = 6;
strcpy(this-&gt;name, "Activity");

Whereby `this-&gt;num` is equivalent to `(*this).num`, and the same is true for it in
the proceeding line. In other words, it makes the syntax slightly less involved.

For further reading on more complex situations, such as those that arise when you want to access
things from <a class="reserve" href="vac.html">multiple structs</a>; Although you can always
return to it once you've gotten more familiar w/ using structs and pointers...

Lets try simply declaring a struct now:
<span class="alt-text">
struct example newA;</span>

Expand Down Expand Up @@ -254,7 +274,7 @@
members of the anonymous struct or union are accessed directly as if
they were members of the containing struct or union.

more links on c standards are <a class="reserve" href="std.html">here</a>
next, see <a class="reserve" href="macro.html">macros</a>
More links on C Standards are <a class="reserve" href="std.html">here</a>

next, see <a class="reserve" href="macro.html">macros</a>
</body></html>
75 changes: 75 additions & 0 deletions vac.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<!-- other -->
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>other example</title>
<meta content="width=device-width,initial-scale=1,user-scalable=no" name=viewport>
<script src="style.js" defer></script>
</head><body>
This is a tricky thing to grok at first, so i'll try to illustrate it like this...
Now, hopefully you already read and learned about how the dot &amp; arrow operators
work as pointers...
Consider we have this sortve structure...
<span class="alt-text">
typedef struct {
char *buffer;
} Anar;

typedef struct {
Anar nar;
} Vac;

typedef struct {
Vac *pack;
} Database;</span>

`Anar` contains a single member, `buffer`, which is a pointer to a character array.
`Vac` contains a single member, `nar`, which is of type `Anar`.
`Database` contains a single member, `pack`, which is a pointer to a `Vac`.

To access the buffer member of `Anar` (which is nested within `Vac`) and then within Database,
you need to handle the pointers and structs in the right way....

Consider we have a `database` which is a pointer to a Database struct, possibly like this...
<span class="alt-text">
Database *database = (Database *)malloc(sizeof(Database));
if (database == NULL) {
perror("Failed to allocate memory for Database");
return 1;
}</span>

Which is for allocating and initializing a `Database` instance. Here's some things we might do...
`database-&gt;pack` accesses the `pack` member of `Database`, which is a pointer to a `Vac` struct.
`*database-&gt;pack` would dereference the pointer, yielding the actual `Vac` struct.
`(*database-&gt;pack).nar` would access the `nar` member of the `Vac` struct.
`(*database-&gt;pack).nar.buffer` accesses the `buffer` member of the `Anar` struct.

It sounds more complicated than it is, since its describing multiple scenarios, however you can
take it from the last scenario, and sortve trace the steps backwards so to speak...

The last scenario, we said we could take that `(*database&gt;pack)` to access the `buffer` member of the `Anar` struct.

In C, when you mix (.) period and (-&gt;) arrow operators with dereferencing, parentheses adheres the order of
operations. Without parentheses, you can’t directly use the (.) operator on a pointer without first dereferencing it.

For example, consider *database-&gt;pack.nar.buffer Here’s how the C compiler interprets it:

`database-&gt;pack` is a pointer to Vac.
`database-&gt;pack.nar` is an invalid syntax because (-&gt;) should be used for accessing members from a pointer
to a struct, and `nar` is not a member of ` Vac * ` but of `Vac`

To correctly access buffer, you need to dereference `database-&gt;pack` to get the `Vac` struct.
Access the `nar` member of the `Vac` struct. And access the `buffer` member of the `Anar` struct (as we said when describing the last scenario)

So the correct syntax in that particular situation would be:
<span class="alt-text">
(*database-&gt;pack).nar.buffer
</span>

At some point you might even consider simplifying access with a temporary variable
<span class="alt-text">
Vac *exac = *database-&gt;pack;</span>

Which would allow you to use it as `exac-&gt;nar.buffer`

0 comments on commit 6b0b750

Please sign in to comment.