Part 10 - Structures
--------------------
Welcome to Part 10 of this tutorial. We will be dealing with structures
in this portion. Structures are analogous to records in other languages
such as Pascal.
Structures are a wonderful feature of the C language. A structure is a
collection of variables grouped together under a single name. You can
use variables of any type inside a structure. Each seperate variable is
called a member. We'll start with a small example of what a structure
definition looks like. Later on, I'll show you a structure that I used
while developing a simple game a few years ago.
struct point {
int x;
int y;
};
The name of the struct is "point". The name is sometimes called the
"tag". We define our variables inside the braces. The variables
inside the braces are called members. Each member has it's own type.
It would be perfectly acceptable for me to use floats or doubles, or
both if required.
We have just defined a new type. This new type is called "point" and
it has two members. Now that it is defined, we can declare variables
of "point" like so:
struct point P1;
You declare a variable of type "point" almost the same way you do other
variables, with the exception of the word "struct" to indicate that the
type "point" is a structure. The new variable that we have defined is
called "P1". It is of type "point".
There is another way to declare variables of your new type. You can
place the variables you want to declare (in our case "P1") immediately
following the closing brace of the structure definition. For example:
struct point {
int x;
int y;
} P1, P2; /* declare 2 variables of type "point", P1, P2 */
If you are not going to declare your variables immediately after the
structure definition, make sure you add the semi-colon after the
closing brace.
We still have the problem of how to access those variables inside our
structure. We use the member selection operator. Also known as the
dot operator. For example, to give P1.x and P2.x values, we can do
this:
P1.x = 5;
P2.x = 99;
We can do the same with "y".
I like to use structures to group similiar information together. For
example, many years ago, I started developing a simple game. I needed
to store information about weapons and armour etc. I used a struct.
Here it is:
struct WeapRec {
char Name[15+1]; /*Name is 15 chars max with 1 reserved for \0 */
int Price;
int MaxDamage, MinDamage;
} Weapon; /*End of Weapon structure.*/
With this structure you could set the price of a weapon like so:
Weapon.Price = 1200;
You could display it as well: printf("Price is %d", Weapon.Price);
The original game was written in Pascal in the early 90's. I tried to
port it to C, but had written it so poorly in Pascal that I just
couldn't finish translating it. It was unfinished as it was anyhow.
The code is locked away, never to be seen by man/woman ever again. I
hope. The last thing that I'll do is show you a program that uses
structures and walk you through it.
/*------------------------------------------------------------------
Program: STRUCTS.C
Author: Ian Patterson
Date: Oct. 12, 2001
Description: This program is merely a demonstration of how to
properly use structures in a program.
------------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
struct PeopleInfo {
char FName[20];
char LName[25];
int age;
};
int main(void)
{
struct PeopleInfo person;
printf("Please enter the persons first name: ");
fgets(person.FName, sizeof(person.FName), stdin);
printf("Please enter the persons last name: ");
fgets(person.LName, sizeof(person.LName), stdin);
printf("Please enter this persons age: ");
fscanf(stdin, "%d", &person.age);
printf("The First name is: %s", person.FName);
printf("The Last name is: %s", person.LName);
printf("You entered %d for the age of this person.\n", person.age);
return EXIT_SUCCESS;
}
That's all I have to say about structs. Actually, it's not. But this
isn't a "show you everything" tutorial. For more info, get a book.
We will discuss files in the next section: Part 11.