You are on page 1of 20

Fundamentals of Computer Science

Lecture 24: Structures


Ethan Cerami
New York University
Road-Map
• What is a Structure?
• Declaring / Initializing Structures
• Working with Structures
• Pointing to Structures

• Reading: Chapter 10, 10.1 – 10.5


What is a Structure?
Defining Structures
• What’s an Array?
– an aggregate of data.
– all data must be of same data type.
– for example, an array must contain all floats or
all integers.
• What’s a structure?
– an aggregate of data.
– can include multiple data types.
– for example, a structure can contain a few
floats, a few integers, a few strings, etc.
Why are structures important?
• Structures enable the creation of databases.
– For Example:
• Address Books, Libraries, Financial Applications
• Structures also enable the creation of “dynamic data
structures”:
– Examples:
• stacks
• queues
• trees, etc.
• (all covered in intermediate computer science
courses.)
Declaring / Initializing Structures
Structure Templates
• To create a structure, you must first create a structure
template.

• Example: Creating an employee database for a company.

struct employee { Structure Members:


char first_name[MAXCHARS]; • Can be of any data type (int,
char last_name [MAXCHARS]; float, long, etc).
int employee_id; • Can also include arrays, pointers
or even other structures.
float hourly_wage;
};

• Here, we are mixing character strings with integers and a


float.
Declaring Structure Variables
• Once you have a structure template, you can create
structure variables.
• Example 1:
struct employee PersonA;
– Creates a variable called PersonA, which contains the
structure template defined by employee.
• Example 2:
struct employee Company[50];
– Creates an array of 50 employee structures. Good for
creating a complete database of all company employees.
Short-Cut Approach
• Short-cut: You can declare a template and a list of
variables all at one:

struct employee {
char first_name[MAXCHARS];
char last_name [MAXCHARS];
int employee_id;
float hourly_wage;
} employeeA, employeeB;

• If you only plan on creating employeeA and employeeB,


you can safely omit the employee tag name.
Initializing Structures
• Initializing structures is very similar to initializing
arrays: just plan all data within { }
• Examples:

struct employee personA = {"Bill", “Smith", 5, 25.0};


struct employee personB = {“John”, “Doe”, 6, 100.0};
Working with Structures
Accessing Members of a Structure
• To access the member of a structure, use the
structure member operator (also called the dot
operator).
• General template is as follows:

variable_name.structure_name;
Examples
• Example 1: To get the employee ID # for
Employee A.

printf ("Employee ID:\t%d\n", employeeA.employee_id);

• Example 2: To get the hourly wage for Employee


A.

printf (“Hourly Wage:\t%.2f\n", employeeA.hourly_wage);


Example
• The next slide shows a full example of using
structures within a program.
• The program uses the employee structure defined
earlier.
#include <stdio.h>
#define MAXCHARS 255

struct employee {
char first_name[MAXCHARS];
char last_name [MAXCHARS];
int employee_id;
float hourly_wage;
};

void print_data (struct employee);

int main () {
struct employee personA = {"Bill", "Smith", 5, 25.0};
struct employee personB = {"John", "Doe", 6, 100.0};

print_data (personA);
print_data (personB);
return 0;
}
void print_data (struct employee person) {
printf ("Name: \t\t%s %s\n", person.first_name, person.last_name);
printf ("Employee ID:\t%d\n", person.employee_id);
printf ("Hourly Wage:\t%.2f\n", person.hourly_wage);
}

Name: Bill Smith


Employee ID: 5
Hourly Wage: 25.00
Name: John Doe
Employee ID: 6
Hourly Wage: 100.00
Pointing to Structures
Pointing to Structures
• Just like regular variables, you can create pointers
to structures.

struct employee personA = {"Bill", “Smith", 5, 25.0};


struct employee *employeePtr = NULL;
employeePtr = &personA;

First: Bill
Last: Smith
ID: 5
employeePtr Wage: 25.0
Accessing Members via Pointers
• To access a structure member, use the structure pointer
operator (also called the arrow operator.)

pointer_var->member_name;

printf ("EmployeeID: %d", employeePtr->employee_id);

• Equivalent to:

printf ("EmployeeID: %d", (*employeePtr).employee_id);

• But, the arrow notation is easier to understand and more


commonly used.
#include <stdio.h>
#define MAXCHARS 255
struct employee {
char first_name[MAXCHARS];
char last_name [MAXCHARS];
int employee_id;
float hourly_wage;
};
void print_data (struct employee*);
main () {
struct employee personA = {"Bill", “Smith", 5, 25.0};
struct employee personB = {“John", “Doe", 6, 100.0};
print_data (&personA);
print_data (&personB);
return 0;
}
void print_data (struct employee *person) {
printf ("Name: \t\t%s %s\n", person->first_name, person->last_name);
printf ("Employee ID:\t%d\n", person->employee_id);
printf ("Hourly Wage:\t%.2f\n", person->hourly_wage);
}

You might also like