You are on page 1of 11

Unit 8: Structures

Data Structures (struct)

• Arrays require that all elements be of the same data


type.
• It is necessary to group information of different data
types: list of products (productid, name, dimensions,
weight, cost)
• Structures can store combinations of character,
integer floating point and enumerated type data.
Name of the data type : struct.

2
Structures (struct)
• A struct is a structured data type composed of
members that are each standard or structured data
types.

• A single struct would store the data for one object.


An array of structs would store the data for several
objects.

• A struct can be defined in several ways as illustrated


in the following examples:

3
Declaring Structures (struct)
Does Not Reserve Space Reserves Space
struct my_example struct my_example
{
{
int label;
int label;
char letter;
char name[20]; char letter;
}; char name[20];
/* The name "my_example" } mystruct ;
is called a structure tag
*/

4
User Defined Data Types
(typedef)
typedef : for creating synonyms for previously defined data
type names.
Example:
typedef int Length;
Length is a synonym (alias) for the data type int.

The data “type” name Length can now be used in


declarations in exactly the same way that the data type int
can be used:
Length a, b, len ;
Length numbers[10] ;

5
Typedef & Struct
 Often, typedef is used in combination with struct to declare a
synonym (or an alias) for a structure:

typedef struct /* Define a structure */


{
int label ;
char letter;
char name[20] ;
} Some_name ; /* The "alias" is Some_name */

Some_name mystruct ; /* Create a struct variable */

6
Accessing Struct Members (Fields)
 Individual members of a struct variable may be
accessed using the structure member operator (the dot,
“.”):
mystruct.letter ;
 Or , if a pointer to the struct has been declared and
initialized
Some_name *myptr = &mystruct ;
by using the structure pointer operator (the “->“):
myptr -> letter ;
which could also be written as:
(*myptr).letter ;

7
Sample Program With Structs
/* This program illustrates creating structs and then declaring and using
struct variables. Note that struct personal is an included data type in
struct "identity". */
#include <stdio.h>
struct personal /*Create a struct but don’t reserve space*/
{ long id;
float gpa;
};
struct identity /*Create a second struct that includes the first one. */
{ char name[30];
struct personal person;
};

8
Sample Program With Structs
(cont.)
int main ( )
{
struct identity js = {"Joe Smith"}, *ptr = &js ;

js.person.id = 123456789 ;
js.person.gpa = 3.4 ;
printf ("%s %ld %f\n", js.name, js.person.id,
js.person.gpa) ;
printf ("%s %ld %f\n", ptr->name, ptr->person.id,
ptr->person.gpa) ;
}

9
EXERCISE
• Suppose we have the user-defined structure baby,
including:
• Name is a string
• BirthYear is an integer between 2015 and 2019
• BirthMonth is an integer between 1 and 12
• Weight is a real number between 0 and 10
• Sex is an integer that receives only 2 values :1 for boys and 0
for girls
• Read information about n babies (1<=n<=10) into array
B
• Read information about babies until we get weight 0 .

10
• Read month M (1 ≤ M ≤ 12) and year Y (2015 ≤ Y ≤ 2019)
• Display information of babies that were born in month M of year Y with the
following format:
Name Weight Sex
Suri Cruise 3.45 F
Romeo Beckham 4.35 M

• Name is displayed in a 40 character space, left justified


• Weight is displayed using ten characters with two digits after the decimal point,
right justified.
• Sex is displayed with ‘M’ and ‘F’
• Write message “No baby was born in the month M/Y” if no baby matches.

11

You might also like