"The word 'radical' derives from the Latin word for root. Therefore, if you want to get to the root of anything you must be radical. It is no accident that the word has now been totally demonized..." -- Gore Vidal
Pre-face: Welp, it's about that time for me to contribute to the legacy that is HTS. I figured that I would start off with some quasi-advanced C tutorials as many of you seemed to take an interest in that. Though in the thread I did say I would make an animation and crap, it turns out I hate my voice and can't draw. So, this is the next best thing. Enjoy.
Disclaimer: If you (the readers) don't know any programming, at all. Stop, learn, come back. If you don't know any C at all, stop, learn, and come back. I will do my best to explain things, but I won't be explain every little thing. Sorry.
Abstract... Sort of:
In the world of C programming, there's a mystical thing called a struct. These magical things are defined by Wikiepida as:
A struct in C programming language is a structured (record) type that aggregates a fixed set of labelled objects, possibly of different types, into a single object.
I'll try my best to make this readable for those who don't read the dictionary for fun:
A struct in C is a way of making a bunch of different variables easily accessed through one variable"
(Feel free to quote (only in flatering situations))
If you still don't understand that, then this tutorial is for you!
There's no reason for you to read this, I just wanted to see how many different sections I could write without actually doing any work...
The answer is 5.
Advanced C tutorials 1: Basic understanding of structs.
Alright yall, let's get this party started with some programming! So, fire up your favorite IDE and let's go. The basic syntax of a struct is as follows:
Alright, let's step through this piece by piece:
So, this is a normal variable declaration. We have the variable type (struct) and the name of said variable (structName (feel free to change)). Alright, looks like we got that down, pretty simple. Moving on.
Well, unlike most other variables, we suddenly have brackets! Whoa! Cool your tits, bro. All this is, is where you're going to put your struct variables. What the actual fuck is a struct variable, you say?! All it is, is a variable that is accessed through the struct variable. The variables in structs are just like any other variable, the only thing is that they must be accessed through wherever you initialize your struct. This looks like something below:
I have a feeling you guys are saying something like this, "Wait, you barely explained the first thing, and now you're throwing in another?! AGAIN! WHAT THE FUCK?!". Hush my darlings, I will explain through an amazing analogy:
Let's say you aren't a forever-aloner and actually have friends, for the sake of being bad asses we'll call them "struct(s)". Each struct (Remember, struct = friend) has their own name and their own personal details. They all have properties about them, that nothing else has (Name, age, height, weight, eye color, skin color, etc). However, when talking about these structs, you don't just go around saying, "Hey, black, 4 feet, 120 lbs". It just wouldn't make sense. Instead, you reference (initialize) your struct first, so it actually makes sense. Something like, "Hey, my struct John is black, 4 feet tall, and 120 lbs". Well, after you reference (initialize) John into your conversation (code) you don't have to keep referenceing (intializing) the fact that you two know each other, ("Hey, my struct John is black, 4 feet, and 120 lbs. My struct John also has blue eyes"). After you've already brought John up, you just say, "Hey, my struct John is black, 4 feet, and 120 lbs. John also has blue eyes". This is the same as the code above. Once your struct has been initialized, "struct structName nameHere" (think of "My friend John"), you don't have to keep referencing it. Instead, to access the variables you placed inside (your friends attributes), all you have to do is call the name you assigned with a dot seperater between the assigned name and the requested variable, "nameHere.intName = 8;" (think "John is 8 years old").
And that, my friends, is basic structs in C. Below I have posted a basic example program that uses structs so that you guys can see it all in action, so to speak. If you don't understand something, feel free to PM me (or look it up on Google).
int main() // Our main function
struct bankAccount // Our new struct, with the name 'bankAccount'
int accountID; // Our single struct variable, accountID
struct bankAccount memberJohn; // Making a variable to access our struct variables through
memberJohn.accountID = 42; // Changing our accountID variable to 42
printf("Account ID: %d", memberJohn.accountID); // Printing our variable
return 0; // Returning after sucessful execution
This program outputs:
Account ID: 42
Please note that this was written in order to give you guys a BASIC overview of structs and how to use them. In the next tutorial, I'll go into more advanced uses for them.
Well, that seems to be it, for now. I'll make another one soonish, possibly.
Cast your vote on this article 10 - Highest, 1 - Lowest
Comments: Published: 6 comments.
HackThisSite is is the collective work of the HackThisSite staff, licensed under a CC BY-NC license.
We ask that you inform us upon sharing or distributing.