diff --git a/arr.html b/arr.html index 9a6292c..bc49705 100644 --- a/arr.html +++ b/arr.html @@ -9,6 +9,7 @@
--- fly^p4rray ---
+ int arr[3]; @@ -270,7 +271,8 @@ } 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 index#2, and the element of that position. see malloc on page8, or see struct on page5 diff --git a/index.html b/index.html index 7fd1f14..de50a1f 100644 --- a/index.html +++ b/index.html @@ -17,8 +17,8 @@ 439l ▸pointer 439l ▸variadic function 439l ▸function pointer u graduate from the triathlon upon reaching this point - 278l ▸array ▂___ - 260l ▸struct + 280l ▸array ▂___ + 280l ▸struct 312l ▸macro
→ .-=========-. diff --git a/struct.html b/struct.html index 9ca4880..c76a4b7 100644 --- a/struct.html +++ b/struct.html @@ -13,10 +13,15 @@ in technical terms they would be described as contiguous blocks of memory, wherein the fields (members) are accessed using offsets. + +---------------+ | x | y | -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 @@ -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 (->) 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->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 (->) +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->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. + +List *this = &that; +this->num = 6; +strcpy(this->name, "Activity"); + +Whereby `this->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 multiple structs; 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: struct example newA; @@ -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 here -next, see macros +More links on C Standards are here +next, see macros diff --git a/vac.html b/vac.html new file mode 100644 index 0000000..26acc19 --- /dev/null +++ b/vac.html @@ -0,0 +1,75 @@ + + + + + +other example + + + +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 & arrow operators +work as pointers... +Consider we have this sortve structure... + + typedef struct { + char *buffer; + } Anar; + + typedef struct { + Anar nar; + } Vac; + + typedef struct { + Vac *pack; + } Database; + +`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... + + Database *database = (Database *)malloc(sizeof(Database)); + if (database == NULL) { + perror("Failed to allocate memory for Database"); + return 1; + } + +Which is for allocating and initializing a `Database` instance. Here's some things we might do... +`database->pack` accesses the `pack` member of `Database`, which is a pointer to a `Vac` struct. +`*database->pack` would dereference the pointer, yielding the actual `Vac` struct. +`(*database->pack).nar` would access the `nar` member of the `Vac` struct. +`(*database->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>pack)` to access the `buffer` member of the `Anar` struct. + +In C, when you mix (.) period and (->) 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->pack.nar.buffer Here’s how the C compiler interprets it: + +`database->pack` is a pointer to Vac. +`database->pack.nar` is an invalid syntax because (->) 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->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: + + (*database->pack).nar.buffer + + +At some point you might even consider simplifying access with a temporary variable + + Vac *exac = *database->pack; + +Which would allow you to use it as `exac->nar.buffer` +