0% found this document useful (0 votes)
15 views17 pages

Chapter2 8

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views17 pages

Chapter2 8

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Chapter 8: Structures

Contents

8.1. struct data type


8.2. Accessing struct members
8.3. Copy a struct with typedef
8.4. Array of structs

Nguyen Thi Thu Huong - SoiCT - HUST 2


8.1. struct data type

• Arrays require that all elements be of the same data


type.
• It is necessary to group information of different data
types: list of students (studentid, name, gpa…)
• Structures can store combinations of character, integer,
floating point and enumerated type data. Name of the
data type : struct.

3
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:

4
Declaring Structures (struct)

Reserves Space Does Not Reserve Space


struct student struct student
{ {
char studentid[8]; char studentid[8];
char name[40]; char name[40];
float gpa; float gpa;
} var_student ; } ;
/* The name “student"
is called a
structure tag */

5
8.2. Copy a struct with typedef

• User Defined Data Types (typedef)


• typedef and struct

Nguyen Thi Thu Huong - SoiCT - HUST 6


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] ;

7
Typedef & Struct

 Often, typedef is used in combination with struct to declare a


synonym (or an alias) for a structure:

typedef struct /* Define a no-name structure */


{
char studentid[8];
char name[40];
float gpa;
} student; /* The "alias" of no-name structure is student*/

student var_student ; /* Create a struct variable */

8
8.3.Accessing struct members (Fields)

 Individual members of a struct variable may be accessed using


the structure member operator (the dot, “.”):
var_student.studentid ;

 Or , if a pointer to the struct has been declared and initialized


student *myptr = &var_student;
by using the structure pointer operator (the “->“):
myptr ->studentid
which could also be written as:
(*myptr) .studentid;

9
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 date /*Create a second struct that is included in the
first one. */
{int day, month, year;} ;
struct student /*Create a struct but don’t reserve space*/
{
char studentid[9];
char name[40];
date birthdate;
float gpa;
} ;

10
Sample Program With Structs

int main ( )
{
struct student js = {"20239048","Joe Smith"}, *ptr = &js ;
js.birthdate.day = 7;
js.birthdate.month = 3;
js.birthdate.year = 2003;
js.gpa = 3.67;
printf ("%s %s %d/%d/%d %1.2f\n", js.studentid, js.name,
js.birthdate.day, js.birthdate.month, js.birthdate.year,
js.gpa) ;
printf ("%s %s %d/%d/%d %1.2f\n", ptr->studentid, ptr->name,
ptr->birthdate.day, ptr->birthdate.month, ptr->birthdate.year,
ptr->gpa) ;;
}

11
8.4. Array of structs

• An array whose elements are of type struct is called array of


structs. It is generally useful when we need multiple
structure variables in our program.

Nguyen Thi Thu Huong - SoiCT - HUST 12


Declaration of Array of Structures
struct structure_name array_name
[number_of_elements];
Example:
struct baby /*Create a struct but don’t reserve space*/
{ char Name [40];
int BirthYear,BirthMonth,Sex;
float Weight;
}B[10];
Or
struct baby /*Create a struct but don’t reserve space*/
{ char Name [40];
int BirthYear,BirthMonth,Sex;
float Weight;
} ;
struct baby B[10];

13
struct array example

• 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.
14
struct array example

int main ( )
{
int n,i, M, Y;
struct baby B[10];
do
{printf ("Input number of babies:");
scanf("%d",&n);}
while( n<1 ||n>10);//check n between 1 and 10
for(i=0;i<n;i++) //Input babies’ information

15
struct array example

{printf("\nEnter information of baby No %d: ", i);

fflush(stdin);

printf("\nName");

gets(B[i].Name);

printf("\n Birth month, Birth year");

scanf("%d %d",&B[i].BirthMonth, &B[i].BirthYear);

printf("\n Sex (1 for boys, 0 for girls)");

scanf("%d",&B[i].Sex);

printf("Weight");

scanf("%f",&B[i].Weight);

} 16
struct array example

printf("\n Input a month");


scanf("%d", &M);
printf("\n Input a year");
scanf("%d", &Y);
for(i=0;i<n;i++)
if(B[i].BirthMonth==M && B[i].BirthYear==Y)break;
if(i<n)
{for(i=0;i<n;i++)
if(B[i].BirthMonth==M && B[i].BirthYear==Y)
printf("\n %-40s %10.2f %c
",B[i].Name,B[i].Weight, B[i].Sex?'M':'F');}
else printf("No baby was born in %d/%d", M, Y);
}
17

You might also like