Access_Denied's Forums

Site Resources => Snippets => Topic started by: Access_Denied on August 17, 2007, 06:01:59 AM



Title: Snippet: Structures
Post by: Access_Denied on August 17, 2007, 06:01:59 AM
OK, I was reading some forums posts earlier (on a different forum), and someone said that structures are very important. And since I highly agree with him, here's a quick snippet on structures.

Here's an example:

typedef struct { //Declare that we're about to make a structure
     int a;  //Start of variables in structure
     int b;
     float c;
     char d[2];
     u32 e;
     Image* f; //end of variables in structure
} Example; //end of structure and the structure name

So, how do you use a structure, you ask? Easily, like this:

Example foobar;

Now, since our structure is named example, we use Example to signal we're using our structure, then the name of our created structure is foobar. Now, here's how you use a structure.

foobar.a = 5;
foobar.b = 2;
foobar.c = 2.4;
foobar.d = "a";
foobar.e = 16;
foobar.f = loadImage("Image.png");

Do you see how this works? It's simple:

NameOfStructure.VariableInStructure

That's how you call the variables. So if you wanted to blit the image, do this:

blitAlphaImageToScreen(0,0,32,32,foobar.f,0,0);

Now, you may not understand the use of structures now, but very soon you will. So, I'll see you in the next snippet.


Hosted by www.Geocities.ws

1