Chapter 6 Structures

What is a structure?
A structure is a collection of one or more variables grouped under a single name.

Can variables inside of records be of different types?
Yes, variables inside of records can be of different types.

What do structures help with?
Structures help with organizing complicated data.

What is an example where a structure would be useful?
An example where a structure would be useful is a payroll record for an employee.

Can structures contain variables which are other structures?
Yes, structures can contain variables which are other structures.

What change did the ANSI standard bring to structures in C?
The change the ANSI standard brought to structures in C was defining structure assignment.

What are the four things you can do with assigning structures?
The four things you can do with assigning structures are:

  1. Copying them.
  2. Assigning them to variables.
  3. Passing them to functions.
  4. Returning them.

6.1 Basics of Structures

How would a structure for a point in 2D space be written?
A structure for a point in 2D space would be written like:

struct point {
	int x;
	int y;
};

What keyword is used to introduce a structure declaration?
The keyword used to introduce a structure declaration is struct.

What does the declaration of a struct comprise of?
The declaration of a struct is comprised of a list of declarations enclosed in braces.

What can be used with structs between the struct keyword and the opening brace?
Between the struct keyword and the opening brace, a tag name can be used.

What is the purpose of a tag name for a struct?
The purpose of a tag name for a struct is to be a shorthand for the declaration between the braces.

What are the variables inside of a structure called?
The variables inside of a structure are called members.

Can the names of normal variables and structure tag names or member names be the same?
Yes, the names of normal variables and structure tag names or member names can be the same.

What does the declaration of a structure define?
The declaration of a structure defines a type.

What can come after the closing brace of the declaration of a structure in C?
After the closing brace of the declaration of a structure, a list of variables can come after.

What does a structure declaration do if it isn't followed by a list of variables?
If a structure declaration isn't followed by a list of variables, it just defines a template.

How would you create a variable called pt which uses a struct with the tag name point?
To create a variable called p which uses a struct with the tag name point, you write:

struct point pt;

How do you initialize the members of a struct?
You initialize the members of a struct by following its definition with a list of initializers between braces.

What do the initializers for the members of a struct have to be?
The initializers for the members of a struct have to be constant expressions.

How would you create a variable called pt which uses a struct with the tag name point and assigns 320 and 200 to the x and y struct members respectively?
To create a variable called pt which uses a struct with the tag name point and assigns the 320 and 200 to the x and y struct members respectively, you write:

struct point pt = { 320, 200 };

Which operator is used to access the members of a structure?
To operator used to access the members of a structure is the dot operator (.).

How do you access the x member of pt if pt is a point struct?
To access the x member of pt if pt is a point struct, you write:

pt.x;

...

6.2 Structures and Functions

...