Basics of Structure for C Programming - dummies

Basics of Structure for C Programming

By Dan Gookin

You can think of the C programming language structure as a multivariable, or several variables rolled into one. You use structures to store or access complex information. That way, you can keep various int, char, float variables, and even arrays, all in one neat package.

Basics of struct

A structure isn’t a variable type. Instead, think of it as a frame that holds multiple variable types. In many ways, a structure is similar to a record in a database. For example:

Name
Age
Gambling debt

These three items can be fields in a database record, but they can also be members in a structure: Name would be a string; Age, an integer; and Gambling Debt, an unsigned floating-point value. Here’s how such a record would look as a structure in C:

struct record
{
 char name[32];
 int age;
 float debt;
};

struct is a C language keyword that introduces, defines, or creates a new structure.

record is the name of the new structure being created.

Within the curly brackets dwell the structure’s members, the variables contained in the named structure. The record structure type contains three member variables: a string name, an int named age, and a float value, debt.

To use the structure, you must declare a structure variable of the structure type you created. For instance:

struct record human;

This line declares a new variable of the record structure type. The new variable is named human.

Structure variables can also be declared when you define the structure itself. For example:

struct record
{
 char name[32];
 int age;
 float debt;
} human;

These statements define the record structure and declare a record structure variable, human. Multiple variables of that structure type can also be created:

struct record
{
 char name[32];
 int age;
 float debt;
} bill, mary, dan, susie;

Four record structure variables are created in this example. Every variable has access to the three members defined in the structure.

To access members in a structure variable, you use a period, which is the member operator. It connects the structure variable name with a member name. For example:

printf("Victim: %sn",bill.name);

This statement references the name member in the bill structure variable. A char array, it can be used in your code like any other char array. Other members in the structure variable can be used like their individual counterparts as well:

dan.age = 32;

How to fill a structure

As with other variables, you can assign values to a structure variable when it’s created. You must first define the structure type and then declare a structure variable with its member values preset. Ensure that the preset values match the order and type of members defined in the structure, as shown in Declaring an Initialized Structure.

DECLARING AN INITIALIZED STRUCTURE

#include <stdio.h>
int main()
{
 struct president
 {
 char name[40];
 int year;
 };
 struct president first = {
 "George Washington",
 1789
 };
 printf("The first president was %sn",first.name);
 printf("He was inaugurated in %dn",first.year);
 return(0);
}

Exercise 1: Create a new program by typing the source code from Declaring an Initialized Structure into the editor. Build and run.

You can also declare a structure and initialize it in one statement:

struct president
{
 char name[40];
 int year;
} first = {
 "George Washington",
 1789
};

Exercise 2: Modify your source code from Exercise 1 so that the structure and variable are declared and initialized as one statement.

Though you can declare a structure and initialize a structure variable as just shown, you can get away with that trick only once. You cannot use the technique to declare the second structure variable, which must be done the traditional way, as shown in Declaring an Initialized Structure.

Exercise 3: Add another president structure variable, second, to your code, initializing that structure with information about the second president, John Adams, who was inaugurated in 1797. Display the contents of both structures.

How to make an array of structures

Creating individual structure variables, one after the other, is as boring and wasteful as creating a series of any individual variable type. The solution for multiple structures is the same as for multiple individual variables: an array.

A structure array is declared like this:

struct scores player[4];

This statement declares an array of scores structures. The array is named player, and it contains four structure variables as its elements.

The structures in the array are accessed by using a combination of array and structure notation. For example:

player[2].name

The variable in the preceding line accesses the name member in the third element in the player structure array. Yes, that’s the third element because the first element would be referenced like this:

player[0].name

Arrays start numbering with the element 0, not element 1.

Line 10 in Arrays of Structures declares an array of four scores structures. The array is named player. Lines 13 through 19 fill each structure in the array. Lines 21 through 27 display each structure’s member values.

ARRAYS OF STRUCTURES

#include <stdio.h>
int main()
{
 struct scores
 {
 char name[32];
 int score;
 };
 struct scores player[4];
 int x;
 for(x=0;x<4;x++)
 {
 printf("Enter player %d: ",x+1);
 scanf("%s",player[x].name);
 printf("Enter their score: ");
 scanf("%d",&player[x].score);
 }
 puts("Player Info");
 printf("#tNametScoren");
 for(x=0;x<4;x++)
 {
 printf("%dt%st%5dn",
 x+1,player[x].name,player[x].score);
 }
 return(0);
}

Exercise 4: Type the source code from Arrays of Structures into your editor. Build and run the program. Try to keep the scores to fewer than five digits so that they line up properly.

Exercise 5: Add code to Arrays of Structures so that the display of structures is sorted with the highest score listed first. Yes, you can do this. Sorting an array of structures works just like sorting any other array.

Here’s a hint: Line 27 of the solution looks like this:

player[a]=player[b];

You can swap structure array elements just as you can swap any array elements. You don’t need to swap the structure variable’s members.