C Language Reference for Script Programmers - Structures



what are they?
Most of the scripting languages mentioned in the introduction have some form of structures. Object oriented languages such as VB and Java have a data type called “classes”. Structures are very similar to classes, in fact in C++ which is a superset of C there are only a few differences between a class and a struct. Structs allow you to organize related data into a common data type. For example we might want to create a data type that will hold all of the information related to a keyframe for a particular channel. We’ll want the following information in that data type:

Note that while classes contain function members often called methods, structs do not. Also structs do not have constructors or destructors.

Frame number
Key Value
End Behavior
Spline Type

Since messiah uses floating point frames the Frame Number will need to be a float. The Key Value should either be a float or a double depending on the precision you want. Tension, Continuity and Bias should all be floats. And End Behavior and Spline Type can both be ints ( 1 = reset, 2 = reverse, … or something like that).

Since each of the elements are of different types we can’t use an array to group them together, we need some other method. Ideally we’d like to create a “keyframe” type that would allow us to do something like this:

keyframe   myKey; 

myKey.FrameNumber    = 25.0;
myKey.KeyValue       = 1.24;
myKey.Tension        = 1.0;
myKey.Cont           = 0.0;
myKey.Bias           = 0.5;
myKey.End            = END_BEHAVE_RESET;
myKey.Spline         = SPLINE_TYPE_BEZIER;

Above we create a variable called myKey that is of type keyframe. We then assign values to each of the members of myKey by using the ‘.’ operator to access each element. Let’s look at how we can do this.


defining a new structure
To define the structure outlined in the overview we would use the following code:

typedef struct keyframe_St
float      FrameNumber;
double     KeyValue;
float      Tension, Cont, Bias;
int        End, Spline;

Let’s go through this line by line. The first line means that we are creating a new type that is a structure. The tag keyframe_St is called the structure tag, we don’t really need to go into that. When you create a new structure just use the name of the type you’re defining and append a “_St” to it for the tag name. When you use messiah:develop to create your struct definitions this will all be done for you.

  Following this first line we have, enclosed in curly braces, the definition of the members of the struct. The first line of the members defines a float called FrameNumber, the next line a double called KeyValue. The third line defines three floats for Tension, Cont and Bias, and so on. Finally the last line closes the curly braces and indicated the name of this new type, keyframe.

accessing members
As I alluded to earlier, you access each member in a structure with the “.” operator. The syntax is:


e.g. :

keyframe   myKey;

myKey.FrameNumber = 10.0;

pointers to structures

You start to run into a little trouble when you have a pointer to a structure and you want to access one of the members of the structure that the pointer points to. Take the following: 

keyframe * pkey;
keyframe   real_key;

pkey = &real_key;

Now the question is “how do I access the members of real_key with the pointer pkey? Would the following work?

 pkey.FrameNumber = 10;

Hopefully you realize that it won’t work. The reason is that pkey is a pointer to a keyframe, not a keyframe. Therefore pkey doesn’t have any members to access (remember that pointers only contain an address, nothing else). So by adding the .FrameNumber to the end of pkey I’m trying to access a member that isn’t there.

I can get around that be first dereferencing the pointer to the keyframe, once dereferenced we’re dealing with a keyframe and no longer just a pointer to a keyframe. So how ‘bout this:

*pkey.FrameNumber = 10;

Would that work? The answer is no, but I’ll let you off if you didn’t get that one, it’s a little tricky. The reason it won’t work has to do with the order of operations in C. Don’t worry about it just use parentheses as I mentioned earlier:

(*pkey).FrameNumber = 10;

Viola. That will work. The part in the parentheses evaluates to a keyframe, so the reference to the FrameNumber member will now be successful. That does look a little ugly though doesn’t it? Realizing that it would be kind of a pain to write that all the time, the “->” operator was created. This operator takes the place of the “.” operator when we are dealing with pointers to structures instead of actual structures: 

pkey->FrameNumber = 10; 

is equivalent to the previous line. Looks a lot cleaner huh? You will find that structures are used extensively in C programming and certainly in messiah. I’d highly recommend going through one of the reference books and learning structs inside and out.

Converted from CHM to HTML with chm2web Pro 2.82 (unicode)