Back to DFS's C Page


Structures

Real life and all academic disciplines present situations where data of various types or multiple data items of the same type can be attributed to a single object. For example, a person can possess both a name and an age, which would obviously be stored in different kinds of variables -- a char array for the name and probably an int for the age. In mathematics, a point on a graph would be represented by numbers for the coordinates, both of which could be ints or floats.

These aggregates of information can be stored in unrelated variables with names that have some portion of the names in common. For example, we could use xcoord and ycoord for the coordinates of a point. However, this has the disadvantage of not associating the variables as closely as we would like.

C provides a construct that makes the relationship clear and allows you to give a cover name to the object that you create. This construct is known as a structure and is introduced with the keyword struct.

Two sample programs are provided at the end of this page.

The definition below groups four characteristics of a rectangle into a single struct called rectangle.

struct rectangle
  {
    float l; /* length */
    float w; /* width */
    float A; /* Area */
    float P; /* Perimeter */
  };

What this definition does not do is get any memory allocated -- what it does do is create a new programmer-defined data type. Consequently, no data can be stored in rectangle. To get memory allocated, we need to declare a data structure of this type, such as is done below.

   struct rectangle rect;

This structure consists of four members which can be accessed by means of the dot operator, which is also known as the structure member operator. The following example assigns the value 13.2 to the width member.

   rect.w = 13.2;

Obviously, there needs to be a method for using a pointer to this structure. This is needed, for example, for calls to scanf() and in functions where you wish to change values stored in the structure. The scanf() call below uses the address-of operator to store data in the structure member for the width.

   scanf("%f", &rect.w);

The following declares r to be a pointer to a structure of type rectangle and then assigns to it the address of the rect data structure.

   struct rectangle *r;

   r = ▭

Once the address of the structure has been assigned to the pointer variable, the arrow operator (also known as the structure pointer operator) is used to access individual members. It gets is name from the fact that -> looks like an arrow. Here the product of the length and width members is assigned to the area member.

   r->A = r->l * r->w;

Sample Program 1

This is an interactive program using a function with pass by reference to change values in a struct.

#include <stdio.h>
struct rectangle
  {
   float l; /* length */
   float w; /* width */
   float A; /* Area */
   float P; /* Perimeter */
  };

void calcrect(struct rectangle *);

void main()
{
   struct rectangle rect;
   /* -------------------Introduction------------------ */
   printf("This program calculates the area and perimeter\n");
   printf("of a rectangle after you enter its dimensions.\n");
   printf("When asked to, type in a dimension and hit the ENTER key.\n");
   printf("\n");
   /* -------------------Inputting Data------------------*/
   printf("What is the length of the rectangle? ");
   scanf("%f", &rect.l);
   printf("What is the rectangle's width? ");
   scanf("%f", &rect.w);
   /* -------------------Calculation------------------ */
   calcrect(&rect);
   /* -------------------Printing Results------------------ */
   printf("\n");
   printf("The area of a rectangle with a length of %.2f units and\n", rect.l);
   printf("a width of %.2f units is %.2f square units.\n", rect.w, rect.A);
   printf("Its perimeter is %.2f units.\n", rect.P);
}

void calcrect(struct rectangle *r)
{
   r->A = r->l * r->w;
   r->P = 2 * (r->l + r->w);
}

Back to Top

Sample Program 2

This program uses fscanf() to fill an array of structures from a file and a function with pass by value to calculate and return the slope of a line.

#include <stdio.h>

/* Global structure declaration */
struct point
  {
    float x;
    float y;
  };

/* function declaration */
float slope(struct point p1, struct point p2);

int main()
{
   struct point p[2];
   float m;
   FILE *fpin;

   fpin = fopen("sss", "r");
   /* Read first pair, one coordinate at a time */
   fscanf(fpin, "%f", &p[0].x);
   fscanf(fpin, "%f", &p[0].y);
   /* Read second pair, both coordinates at one time */
   fscanf(fpin, "%f%f", &p[1].x, &p[1].y);
   fclose(fpin);

   m = slope(p[0], p[1]); /* pass the line to the slope function */

   printf("The slope of the line is %.2f.\n", m);

   return 0;
}

float slope(struct point p1, struct point p2)
{
   return((p2.y - p1.y) / (p2.x - p1.x));
}

Back to Top


Created 8 Jan 99
Modified 11 Jan 99
© DFStermole: 1999