Sunday, 28 October 2012

Data structures - part 1

Continuing on from the previous post, no, I did NOT just sit there and cry but instead, I went on the internet and found out about data structures and pointers. Apparently, I'll have to make my own ArrayList substitute and I'll need to know about these two things.

So here is a basic introduction to data structures.

In C, the basic user-defined type is the structure (keyword struct). First, you need to define a template  and then declare variables having the new type.

At this point, I almost cried out out loud, "But that's just like classes and objects in Java!" And indeed, there are loads of similarities between structures in C and classes in Java.

Do note that I am making all this stuff up so it might all be nonsense, but let's carry on...

The way to define a structure is as follows:
struct <type-name> {
    <primitive variables>
}; //Don't forget the semi-colon at the end!!!!!!

For example, a piece of music would have a name and a composer (among other things) so a structure defining a piece of music would look something like this:
struct pOMusic {
    char *name;
    char *composer;
};

Then, to declare a variable of type pOMusic, you write this:
struct pOMusic moonlightSonata; //moonlightSonata is the variable name

To set the name and composer of the piece of music write either:
moonlightSonata.name = "Moonlight Sonata";
moonlightSonata.composer = "Beethoven";
OR:
moonlightSonata = {"Moonlight Sonata", "Beethoven"}; //This is like array notation

I could also have just written:

struct pOMusic {
    char *name;
    char *composer;
} moonlightSonata = {"Moonlight Sonata", "Beethoven"}; //Again, DON'T forget the semi-colon at the end!!!!!!

Yeah, so... VERY similar to how Java classes and objects work (except for some major differences.... *ahem*...).

Here is a full working program demonstrating many of the things described above along with some other stuff:

Original Code:

#include <stdio.h>

int main() {
    struct film {
        char *name;
        int ratingOutOfTen;
    } charlieAndTheChocolateFactory = {"Charlie and the Chocolate Factory", 8};
    struct film skyfall, theLorax;
    skyfall.name = "Skyfall";
    skyfall.ratingOutOfTen = 10;
    theLorax.name = "The Lorax";
    theLorax.ratingOutOfTen = 11; //yes, this was intentional...
    
    struct film iceAge = {"Ice Age", 7};
        
    printf("Let the %s, when it crumbles, we will stand tall, face it all together... %s's rating is %d/10 while the film with the worst rating is %s. I like %s but its rating is only %d out of ten.", skyfall.name, theLorax.name, theLorax.ratingOutOfTen, iceAge.name, charlieAndTheChocolateFactory.name, charlieAndTheChocolateFactory.ratingOutOfTen);
    
    return 0;
}

Output:

Let the Skyfall, when it crumbles, we will stand tall, face it all together... The Lorax's rating is 11/10 while the film with the worst rating is Ice Age. I like Charlie and the Chocolate Factory but its rating is only 8 out of ten.


No comments:

Post a Comment